在博客页面上获取最新帖子

时间:2019-06-28 14:58:56

标签: php wordpress wordpress-theming

因此,我正在寻找一种获取最新帖子的方法,以便以不同于其余帖子的方式显示它。在设置中,我可以像通常每个人一样在“博客”页面上显示帖子。

我尝试做的第一件事(另一个问题的答案)是使用普通循环,我的意思是 if(have_posts())... while(have_posts()).. etc 。在该 IF 上方,放置另一个 IF 只是为了获取最新帖子,通过这种方式,我可以为上一篇帖子设置样式。但是,由于我有分页,所以每页上的最新帖子实际上是该页面上的最新帖子,而不是真正的最新帖子。希望这是可以理解的。

我的第二次尝试是从正常循环中排除最新文章,为此,我使用了一篇文章的摘要,该文章解释了如何排除最新文章并使用 pre_get_posts 使分页正常工作和 found_posts ,所以我的代码如下:

add_action('pre_get_posts', 'myprefix_query_offset', 1 );
function myprefix_query_offset(&$query) {

    //Before anything else, make sure this is the right query...
    if ( ! $query->is_home() ) {
        return;
    }

    //First, define your desired offset...
    $offset = 1;

    //Next, determine how many posts per page you want (we'll use WordPress's settings)
    $ppp = get_option('posts_per_page');

    //Next, detect and handle pagination...
    if ( $query->is_paged ) {

        //Manually determine page query offset (offset + current page (minus one) x posts per page)
        $page_offset = $offset + ( ($query->query_vars['paged']-1) * $ppp );

        //Apply adjust page offset
        $query->set('offset', $page_offset );

    }
    else {

        //This is the first page. Just use the offset...
        $query->set('offset',$offset);

    }
}

add_filter('found_posts', 'myprefix_adjust_offset_pagination', 1, 2 );
function myprefix_adjust_offset_pagination($found_posts, $query) {

    //Define our offset again...
    $offset = 1;

    //Ensure we're modifying the right query object...
    if ( $query->is_home() ) {
        //Reduce WordPress's found_posts count by the offset...
        return $found_posts - $offset;
    }
    return $found_posts;
}

到目前为止,该代码可以正常工作,并且不包括最新文章,并且分页也有效,但是现在我的问题是,如何获得最新文章?我试图在home.php循环中的上方使用 wp_query 来获取该条最新帖子,但我意识到 pre_get_posts 会覆盖 wp_query 吗?

我该如何解决并获得最新帖子?我必须做相反的事情吗?我的意思是,首先获取最新帖子,然后为其余帖子创建自定义循环,但是如何管理分页?

这是到目前为止我在home.php中的html

<div class="blog-list">

  <div class="blog-item featured-item">
    // display latest post here
  </div>
  <?php if ( have_posts() ): ?>

    <div class="teaser-inner mb-4">
        <div class="row">

            <?php while ( have_posts() ): ?>

                <?php the_post(); ?>

                <div class="blog-item col-md-6 mb-3 mb-sm-5">
                    <div class="row">
                        <div class="col-6 col-md-4">
                            <?php $img_url = get_the_post_thumbnail_url(get_the_ID(),'full'); ?>
                            <a href="<?php echo esc_url( get_permalink() ); ?>" class="blog-link">
                                <div class="blog-item-img bg-cover" style="background-image:url(<?php echo esc_url($img_url); ?>)"></div>
                            </a>
                        </div>
                        <div class="col-6 col-md-8">
                            <a href="<?php echo esc_url( get_permalink() ); ?>"><?php the_title(); ?></a>
                        </div>
                    </div>
                </div>

            <?php endwhile; ?>

        </div>
    </div>

    <div class="blog-pagination">
        <?php echo paginate_links(); ?>
    </div>

  <?php endif; ?>
</div>

任何线索都值得赞赏。

1 个答案:

答案 0 :(得分:1)

您的分页系统运行正常。您可以做的是,它使用wp_get_recent_posts检索最新帖子:

$latestPosts = wp_get_recent_posts(['numberposts' => 1]);
$latestPost = $latestPosts ? current($latestPosts) : null;

然后在您的div中,您可以使用$latestPost显示最新帖子的信息。

<?php if ($latestPost): ?>
    <div class="blog-item featured-item">
        // display latest post here
    </div>
<?php endif; ?>

为使上述方法起作用,您需要稍微修改pre_get_posts以确保仅在主要查询条件下才更改查询:

if (! $query->is_home() || ! $query->is_main_query()) {
    return;
}
相关问题