2016-03-02 125 views
0

我使用Wp_query()獲取帖子。然後顯示post_thumbnail和標題。如何在Wordpress中顯示關於帖子的簡短描述?

<?php 

$args = array(
    'type' => 'post', 
    'category__in' => '23', 
    'posts_per_page' => 1, 
    'offset' => 2, 
); 

$lastBlog = new WP_Query($args); 
if ($lastBlog->have_posts()): 
    while ($lastBlog->have_posts()): $lastBlog->the_post(); 

     if (has_post_thumbnail()) { 
      the_post_thumbnail(); 
      the_title(sprintf('<h4 class="entry-title"><a href="%s">', esc_url(get_permalink())), '</a></h4>'); 
     } 

    endwhile; 
endif; 
wp_reset_postdata(); 
?> 

如果我想在標題下顯示關於該帖子的簡短描述並插入閱讀更多,我該怎麼做?

謝謝

回答

1

有兩種方法可以做到這一點。一種方法是文章的只是輸出的內容,並提出了「更多」的文本<!--more-->標籤,並且之後你會得到與「更多」按鈕,輸出的文本:

<?php 
$args = array( 
    'type' => 'post', 
    'category__in' => '23', 
    'posts_per_page' => 1, 
    'offset' => 2, 
    ); 

$lastBlog = new WP_Query($args); 

if($lastBlog->have_posts()): 
    while($lastBlog->have_posts()): $lastBlog->the_post(); 
     if (has_post_thumbnail()) { 
    echo '<div class="post_wrapper">'; 
    the_post_thumbnail(); 
    the_title(sprintf('<h4 class="entry-title"><a href="%s">', esc_url(get_permalink())),'</a></h4>'); 
    the_content('Read more ...'); 
    echo '</div>'; 
     } 
    endwhile; 
endif; 

wp_reset_postdata();  
?> 

我已將所有內容都包裹在.post_wrapper div中,以便於處理。

另一種方法是使用the excerpt()與手動添加的「更多」按鈕

<?php 
$args = array( 
    'type' => 'post', 
    'category__in' => '23', 
    'posts_per_page' => 1, 
    'offset' => 2, 
    ); 

$lastBlog = new WP_Query($args); 

if($lastBlog->have_posts()): 
    while($lastBlog->have_posts()): $lastBlog->the_post(); 
     if (has_post_thumbnail()) { 
    echo '<div class="post_wrapper">'; 
    the_post_thumbnail(); 
    the_title(sprintf('<h4 class="entry-title"><a href="%s">', esc_url(get_permalink())),'</a></h4>'); 
    the_excerpt(); 
    echo '<a href="'.esc_url(get_permalink()).'" class="read_more_button" title="'.esc_html__('Read more...', 'theme_slug').'">'.esc_html__('Read more...', 'theme_slug').'</a>'; 
    echo '</div>'; 
     } 
    endwhile; 
endif; 

wp_reset_postdata();  
?> 

您可以選擇使用哪一個。

另外,如果你沒有縮略圖,你是否確定不想顯示帖子?如果沒有,只要在標題前移動if條件,即使沒有設置發佈縮略圖,也應該有帖子。如果你的意思是這樣,那麼一切都好。 :D

相關問題