我使用Wp_query()获得Post。然后显示post_thumbnail和标题。
<?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();
?>
如果我想在标题下显示关于帖子的简短描述并插入阅读更多,我该怎么做?
由于
答案 0 :(得分:2)
有两种方法可以做到这一点。一种方法是只输出文章的内容,然后放一个“阅读更多”的内容。在<!--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()
手动添加&#39;了解详情&#39;按钮
<?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