首页上的其他特色帖子

时间:2017-11-04 作者:Alexander Holsgrove

我正在寻找一种方法,在我博客的第一页上加载一篇特色文章。在这篇文章下面,我需要能够显示12篇以上的文章,然后能够通过分页列表一次点击12篇文章。

到目前为止,我已经将博客页面设置为显示12篇文章,并设置了一个“特色”类别,可以在自定义查询中使用该类别加载最新的带有此术语的文章,就在第一页上。

我只想从主帖子循环中排除这篇最新的帖子,但仍然会在下面12篇的列表中显示较旧的特色帖子。

是否有挂钩或偏移可以实现这一点?

1 个回复
最合适的回答,由SO网友:RossAlexander 整理而成

如果我正确理解您的目标,我会创建两个queries. 第一个将只检索特色帖子。我们将仅在第一页显示该帖子,并存储其ID以供以后使用。然后,第二个查询将获得所有剩余的帖子。

// first query to retrieve the featured post
$featured_post_id = 0; // later we\'ll store the featured post\'s ID here
if(!is_paged()){ // ensure we only see the feature post on the first page
    $first_args = array(
        \'post_type\'      => array(\'post\'), // change to custom post type if using one
        \'post_status\'    => array(\'publish\'), // only get the latest published post
        \'posts_per_page\' => 1, // we\'ll only get one post
        \'cat\'            => 666, // put the ID of the \'featured\' category here
    );

    $first_query = new WP_Query( $first_args );
    if($first_query->have_posts()) {
        while($first_query->have_posts()) {
            $first_query->the_post();
            $featured_post_id = get_the_ID();
            // your code for displaying the featured post here
        }
    } else {
        return;
    }
}

// second query to retrieve remaining posts (excluding featured post from above)
$paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 1;
$second_args = array(
    \'post_type\'         => array(\'post\'),
    \'post_status\'       => array(\'publish\'),
    \'posts_per_page\'    => get_option( \'posts_per_page\' ), // will retrieve number of posts based on value set in Wordpress\'s admin > Settings > Reading
    \'paged\'             => $paged, // use pagination
    \'post__not_in\'      => array($featured_post_id), // exclude featured post from first query
);

$second_query = new WP_Query( $second_args );
if($second_query->have_posts()) {
    while($second_query->have_posts()) {
        $second_query->the_post();
        // your code for displaying remaining posts here
    }
} else {
    return;
}
the_posts_pagination(); // your pagination code here

结束

相关推荐

Twenty Seventeen Pages Loop

有人能向我解释一下,在静态主页的2017主题中,它在哪里被引导去循环页面而不是帖子?我不明白它是在哪里这样做的,从一个新的WordPress安装。在里面front-page.php 我有这个:<?php // Get each of our panels and show the post data. if ( 0 !== twentyseventeen_panel_count() || is_customize_preview() ) : // If