PHP前端开发

每个类别显示最新的帖子

百变鹏仔 4周前 (09-22) #HTML
文章标签 类别

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

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

您需要什么

要学习本教程,您需要:

设置主题

第一步是设置主题。我将创建“二十四”主题的子主题,仅包含两个文件:style.css 和 index.php。

这是我的样式表:

/*Theme Name: Display the Most Recent Post in Each CategoryTheme URI: http://code.tutsplus.com/tutorials/display-the-most-recent-post-in-each-category--cms-22677Version: 1.0.0Description: Theme to accompany tutorial on displaying the most recent post fort each term in a taxonomy for Tutsplus, at http://bitly.com/14cm0ybAuthor: Rachel McCollinAuthor URI: http://rachelmccollin.co.ukLicense: GPL-3.0+License URI: http://www.gnu.org/licenses/gpl-3.0.htmlDomain Path: /langText Domain: tutsplusTemplate: 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;}

现在内容更适合页面并且布局更好:

使此技术适应不同的内容类型

您可以调整此技术以处理不同的内容类型或分类法。例如:

摘要

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