微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

每个类别显示最新的帖子

认情况下,您的 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 数组中。

摘要

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

以上就是每个类别显示最新的帖子的详细内容,更多请关注编程之家其它相关文章

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐