每个类别显示最新的帖子

WBOY
WBOY 原创
2023-08-31 11:29:05 274浏览

默认情况下,您的 WordPress 主博客页面按日期降序显示您最近的帖子。但是,如果您在网站上使用类别,并且您的读者想要查看每个类别中的新内容,您可能希望您的博客页面看起来有所不同。

在本教程中,我将向您展示如何做到这一点。我将演示如何:

  • 识别您博客上的所有类别
  • 显示每条帖子的最新帖子,如果帖子有一个,则显示特色图片
  • 确保多个类别的帖子不会重复
  • 添加一些样式使其看起来不错

您需要什么

要学习本教程,您需要:

  • WordPress 的开发安装。
  • 已设置一些帖子和类别。我使用了 WordPress 主题单元测试数据中的数据示例。
  • 一个主题。我将创建“二十四”主题的子主题。
  • 代码编辑器。

设置主题

第一步是设置主题。我将创建“二十四”主题的子主题,仅包含两个文件:style.cssindex.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(); ?>

识别类别

第一步是确定博客中的类别。紧接着打开

标签,添加以下内容:
<?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">&rarr;</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;
    ?>

这会将当前帖子的 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 数组。

保存您的 index.php 文件并再次查看您的博客页面:

每个类别显示最新的帖子

这样更好了!现在您的帖子不再重复。

添加样式

目前,内容有点分散,特色图片位于帖子标题和摘录上方。让我们添加一些样式以使图像向左浮动。

在主题的 style.css 文件中,添加以下内容:

.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 数组中。

摘要

有时,以其他方式(而不是简单地按时间顺序)显示博客上的最新帖子会很有帮助。在这里,我演示了一种技术,用于显示博客上每个类别中的最新帖子,确保帖子在多个类别中不会重复。

以上就是每个类别显示最新的帖子的详细内容,更多请关注php中文网其它相关文章!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。