限制可以设置为精选的帖子数量

时间:2016-11-04 18:14:15

标签: wordpress admin taxonomy featured

这是一个奇怪的问题。这是客户要求的,但我很难找到之前完成的任何示例,所以我主要只是寻找任何提示或跟踪来完成这项工作。

我有一个使用特色邮政分类法构建的wordpress网站。主页设置为仅显示六个特色帖子,默认情况下,它们按照发布的顺序显示。

客户想要的是有某种功能可以限制在任何给定时间可以设置多少帖子。所以理想情况下如何运作的一个例子是有六个特色帖子,管理员进入并设置一个新帖子作为特色。因此,最近最少设置为精选的帖子将自动从特色帖子类别中删除,确保在任何时间只有六个设置。

因此,为了尝试在句子中总结一下,他们想要构建某种功能,这将自动删除该类别中最近的特色帖子,这样他们就不必自己这样做了。

我无法找到之前已经完成的任何示例,如果有人做过类似的事情或者知道我可以在哪里开始学习构建这个功能,那将会有很多帮助。我希望我能够清楚地描述他们正在寻找什么。

2 个答案:

答案 0 :(得分:0)

据我所知,精选帖子不是WordPress核心的一部分,因此处理精选邮政编码将成为您的主题。

如果不知道你的主题是什么,我真的无法提供具体的帮助。

离开我的头顶,创建一个精选的帖子数组,使用count()来跟踪数组中的项目数。使用带有array_unshift()的statments添加到数组的前面,并使用array_pop()从数组后面删除一个帖子,当它超过特色帖子的最大数量时。

这篇文章可能会让您了解如何实现这一目标。 http://www.hongkiat.com/blog/wordpress-featured-content/

联系支持您主题的创建者也是一个不错的选择。

答案 1 :(得分:0)

实际上这并不容易,因为特色帖子功能不是为此而构建的。不过我建议如下:

的伪代码:

// On save post check if the post is featured

// If the post is featured get the number of featured posts from the database

// if the number of featured posts from the database including the current post is greater than six un-feature the post with the oldest timestamp

此代码未经过测试,但应该类似:

add_action('save_post','ensure_only_6_stickies');

function ensure_only_6_stickies($post_id) {

  if(is_sticky($post_id)) {
    $stickies = get_option( 'sticky_posts' );

    if(is_array($stickies) && !in_array($post_id,$stickies) && count($stickies)>=6) {
      $stickies = array_slice($stickies,5);
      array_push($post_id,$stickies);
      update_option('sticky_posts',$stickies);
    }
  }
}