简体   繁体   English

限制WordPress中的粘性帖子数量

[英]Limit the amount of sticky posts in wordpress

I have a website in development and the customer wanted a news and FAQ page. 我有一个正在开发中的网站,客户需要新闻和常见问题解答页面。 Not a problem! 没问题! He wanted to be able to add his own posts (to both news and FAQ. Again, not a problem! The last thing he requested was that he was able to manage the amount of posts showing and what kind of posts that were shown. Now this is where i got confused. I've already build a system where the customer can select a category to show. (i'm using AwesomeBuilder for this). Now we have three different types of posts. Regular, Regular + Sticky, and Sticky. Should the client select the option to only show Regular posts i can turn the sticky posts off by using post__not_in. However if the client selects either Regular + Sticky or Sticky the amount function doesn't work anymore. 他希望能够添加自己的帖子(同时添加到新闻和常见问题解答。同样,这不是问题!他要求的最后一件事是他能够管理显示的帖子数量以及显示的帖子类型。现在我已经在这里建立了一个系统,客户可以选择要显示的类别(为此我正在使用AwesomeBuilder),现在我们有三种不同类型的帖子:常规,常规+粘性和粘性:如果客户选择仅显示常规帖子的选项,我可以使用post__not_in关闭粘性帖子;但是,如果客户选择常规+粘性或粘性,则金额功能将不再起作用。

Lets say the client selects a maximum amount of 10 posts. 假设客户最多选择10个帖子。 At the regular category this gets maxed at 10 and that's it. 在常规类别中,最大值为10,仅此而已。 At the sticky posts however this doesn't happen, it just shows all the sticky posts since stickies are told to always stay on top. 但是,在粘帖中并不会发生这种情况,它只是显示所有粘帖,因为告诉粘帖要始终位于最上面。 Regular + Sticky would show all the sticky posts followed by 10 regular posts. 常规+粘性将显示所有粘性帖子,然后显示10个常规帖子。 I hope my situation and problem is clear. 希望我的情况和问题清楚。

Code below. 下面的代码。

$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. ps。 I know THIS is the about the same question but it hasnt been answered yet. 我知道是关于相同的问题,但尚未得到答复。

确保$ number变量得到10,并确保在query_posts函数中使用$ args,例如query_posts($ args);。

https://codex.wordpress.org/Sticky_Posts https://codex.wordpress.org/Sticky_Posts

You can refer to this "Display just the first sticky post, if none return the last post published: " from the link. 您可以从链接中引用此“仅显示第一个粘性帖子,如果没有则返回最后发布的帖子:”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM