簡體   English   中英

限制WordPress中的粘性帖子數量

[英]Limit the amount of sticky posts in wordpress

我有一個正在開發中的網站,客戶需要新聞和常見問題解答頁面。 沒問題! 他希望能夠添加自己的帖子(同時添加到新聞和常見問題解答。同樣,這不是問題!他要求的最后一件事是他能夠管理顯示的帖子數量以及顯示的帖子類型。現在我已經在這里建立了一個系統,客戶可以選擇要顯示的類別(為此我正在使用AwesomeBuilder),現在我們有三種不同類型的帖子:常規,常規+粘性和粘性:如果客戶選擇僅顯示常規帖子的選項,我可以使用post__not_in關閉粘性帖子;但是,如果客戶選擇常規+粘性或粘性,則金額功能將不再起作用。

假設客戶最多選擇10個帖子。 在常規類別中,最大值為10,僅此而已。 但是,在粘帖中並不會發生這種情況,它只是顯示所有粘帖,因為告訴粘帖要始終位於最上面。 常規+粘性將顯示所有粘性帖子,然后顯示10個常規帖子。 希望我的情況和問題清楚。

下面的代碼。

$sticky = get_option( 'sticky_posts' );
$number = $atts['number'];

if ($atts['sticky'] == 'nieuws') {
    $args = array('post__not_in'  => $sticky, 'posts_per_page' => $number );

} elseif ($atts['sticky'] == 'nieuws-sticky') {
    $sticky_count = count($sticky);
    if ($sticky_count <= $number){
        $number_sticky = $number - $sticky_count;
        $args = array('post_type' => post, 'posts_per_page' => $number_sticky);
    }
    else {
//      $sticky = array_slice($sticky,0, 1);
//      echo 'hello'. $sticky;
        $args = array('post__in'  => $sticky );
    }
} else {
//  $sticky = array_slice($sticky,1, 2);
    $args = array('post__in'  => $sticky, 'posts_per_page' => $number );
}

ps。 我知道是關於相同的問題,但尚未得到答復。

確保$ number變量得到10,並確保在query_posts函數中使用$ args,例如query_posts($ args);。

https://codex.wordpress.org/Sticky_Posts

您可以從鏈接中引用此“僅顯示第一個粘性帖子,如果沒有則返回最后發布的帖子:”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM