第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

獲取博客頁面上的最新帖子

獲取博客頁面上的最新帖子

PHP
繁華開滿天機 2021-09-05 20:45:33
因此,我正在尋找一種獲取最新帖子的方法,以便以與其他帖子不同的方式顯示它。在設(shè)置中,我有我的“博客”頁面來顯示帖子,通常每個人都會這樣做。我嘗試的第一件事(另一個問題的答案)是使用正常循環(huán),我的意思是,if (have_posts())...while(have_posts())..etc。在該IF之上,放置另一個IF以獲取最新帖子,通過這種方式我可以為我的最后一個帖子設(shè)置樣式。但由于我有分頁,在每個頁面上,最新的帖子實際上是該頁面的最新帖子,而不是真正的最新帖子。希望這是可以理解的。我的第二次嘗試是從正常循環(huán)中排除最新帖子,為此我使用了一篇文章中的片段,該文章解釋了如何排除最新帖子并使用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;}到目前為止一切順利,這段代碼正在運行,它不包括最新的帖子并且分頁正在運行,但現(xiàn)在我的問題是,我如何獲得最新的帖子?我嘗試在home.php 中的循環(huán)上方使用wp_query來獲取該單個最新帖子,但意識到pre_get_posts覆蓋了wp_query?我怎樣才能解決這個問題并獲得最新的帖子?我必須做相反的事情嗎?我的意思是,首先獲取最新帖子,然后為其余帖子創(chuàng)建自定義循環(huán),但如何管理分頁?
查看完整描述

1 回答

?
開心每一天1111

TA貢獻1836條經(jīng)驗 獲得超13個贊

您的分頁系統(tǒng)工作正常。您可以做什么,它使用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;

}


查看完整回答
反對 回復 2021-09-05
  • 1 回答
  • 0 關(guān)注
  • 144 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號