2016-03-02 64 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