默认情况下,您的 wordpress 主博客页面按日期降序显示您最近的帖子。但是,如果您在网站上使用类别,并且您的读者想要查看每个类别中的新内容,您可能希望您的博客页面看起来有所不同。
在本教程中,我将向您展示如何做到这一点。我将演示如何:
您需要什么
要学习本教程,您需要:
设置主题
第一步是设置主题。我将创建“二十四”主题的子主题,仅包含两个文件:style.css
和 index.PHP
。
这是我的样式表:
/* Theme Name: display the Most Recent Post in Each Category Theme URI: http://code.tutsplus.com/tutorials/display-the-most-recent-post-in-each-category--cms-22677 Version: 1.0.0 Description: Theme to accompany tutorial on displaying the most recent post fort each term in a taxonomy for Tutsplus, at http://bitly.com/14cm0yb Author: Rachel McCollin Author URI: http://rachelmccollin.co.uk License: GPL-3.0+ License URI: http://www.gnu.org/licenses/gpl-3.0.html Domain Path: /lang Text Domain: tutsplus Template: twentyfourteen */ @import url('../twentyfourteen/style.css');
我稍后会返回此文件来添加样式,但现在 wordpress 只需要识别子主题即可。
创建索引文件
由于我希望我的主博客页面显示每个类别中的最新帖子,因此我将在我的子主题中创建一个新的 index.PHP
文件。
创建一个空的index.PHP文件
首先,我将复制 24 中的 index.PHP
文件,并编辑掉循环和其他内容,使其看起来像这样:
<?PHP /** * The main template file. * * Based on the `index.PHP` file from TwentyFourteen, with an edited version of the `content.PHP` include file from that theme included here. */ ?> <?PHP get_header(); ?> <div id="main-content" class="main-content"> <?PHP if ( is_front_page() && twentyfourteen_has_featured_posts() ) { // Include the featured content template. get_template_part( 'featured-content' ); } ?> <div id="primary" class="content-area"> <div id="content" class="site-content" role="main"> </div> </div> <?PHP get_sidebar( 'content' ); ?> </div> <?PHP get_sidebar(); ?> <?PHP get_footer(); ?>
识别类别
第一步是确定博客中的类别。紧接着打开 这使用 然后我使用 这只会获取当前类别中的一篇帖子。 接下来,使用 这将输出每篇文章的特色图片、标题和摘录,并且每一个都包含在一个链接中。 让我们看看现在的样子: 如您所见,存在问题。我的页面显示每个类别中的最新帖子,但它是重复的帖子,因为有时一个帖子会是多个类别中的最新帖子。让我们解决这个问题。 在添加 这会创建一个名为 这会将当前帖子的 ID 添加到 最后,向查询参数添加一个新参数,以避免输出此数组中的任何帖子。您的论点现在如下所示: 这使用 这样更好了!现在您的帖子不再重复。 目前,内容有点分散,特色图片位于帖子标题和摘录上方。让我们添加一些样式以使图像向左浮动。
有时,以其他方式(而不是简单地按时间顺序)显示博客上的最新帖子会很有帮助。在这里,我演示了一种技术,用于显示博客上每个类别中的最新帖子,确保帖子在多个类别中不会重复。<?PHP
$categories = get_categories();
foreach ( $categories as $category ) {
}
?>
get_categories()
函数来获取博客中的类别列表。默认情况下,这将按字母顺序获取,并且不会包含任何空类别。这对我有用,所以我不会添加任何额外的参数。foreach ( $categories as $category ) {}
告诉 wordpress 依次运行每个类别并运行大括号内的代码。下一步将创建一个针对每个类别运行的查询。定义查询参数
$args = array(
'cat' => $category->term_id,
'post_type' => 'post',
'posts_per_page' => '1',
);
运行查询
WP_Query
类插入查询:$query = new WP_Query( $args );
if ( $query->have_posts() ) { ?>
<section class="<?PHP echo $category->name; ?> listing">
<h2>Latest in <?PHP echo $category->name; ?>:</h2>
<?PHP while ( $query->have_posts() ) {
$query->the_post();
?>
<article id="post-<?PHP the_ID(); ?>" <?PHP post_class( 'category-listing' ); ?>>
<?PHP if ( has_post_thumbnail() ) { ?>
<a href="<?PHP the_permalink(); ?>">
<?PHP the_post_thumbnail( 'thumbnail' ); ?>
</a>
<?PHP } ?>
<h3 class="entry-title">
<a href="<?PHP the_permalink(); ?>">
<?PHP the_title(); ?>
</a>
</h3>
<?PHP the_excerpt( __( 'Continue Reading <span class="Meta-nav">→</span>', 'twentyfourteen' ) ); ?>
</article>
<?PHP } // end while ?>
</section>
<?PHP } // end if
// Use reset to restore original query.
wp_reset_postdata();
避免重复帖子
get_categories()
函数的行上方,添加以下行:$do_not_duplicate = array();
$do_not_duplicate
的空数组,我们将用它来存储每个帖子输出的 ID,然后检查稍后查询的任何帖子的 ID 是否在其中该数组。<?PHP while ( $query->have_posts() ) {
$query->the_post();
$do_not_duplicate[] = $post->ID;
?>
$do_not_duplicate
数组。$args = array(
'cat' => $category->term_id,
'post_type' => 'post',
'posts_per_page' => '1',
'post__not_in' => $do_not_duplicate
);
'post__not_in'
参数来查找帖子 ID 数组。添加样式
.listing h2 {
margin-left: 10px;
}
.category-listing img {
float: left;
margin: 10px 2%;
}
.category-listing .entry-title {
clear: none;
}
使此技术适应不同的内容类型
get_categories()
替换为 get_terms()
并更改 'cat'
查询参数来查找分类术语。'post_type' => 'post'
参数您的查询参数与您的帖子类型。foreach
语句来运行多个循环。single.PHP
页面,以便在帖子内容之后显示每个类别中最新帖子的链接。如果执行此操作,您需要将当前显示页面的 ID 添加到 $do_not_duplicate
数组中。摘要
以上就是每个类别显示最新的帖子的详细内容,更多请关注编程之家其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。