【问题标题】:Wordpress: Find posts in category 1 and not in category 2Wordpress:查找第 1 类而不是第 2 类的帖子
【发布时间】:2010-01-27 04:36:10
【问题描述】:

我有两个类别,visible 和 hideMe...

我想列出可见类别中的所有帖子,这很容易。但是,由于用户错误(或疏忽),可能有一些帖子属于这两个类别。

如何拉取可见类别但不属于 hideMe 类别的帖子?

这是我目前的解决方案。我只是不确定 get_posts 方法是否是我可以使用的最便宜的选择...

$cat=get_cat_ID('visible');
$cat2=get_cat_ID('hideMe');
$myposts = get_posts(array('cat' => "$cat,-$cat2",'showposts' => 5));
if($myposts )
{//do something}

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    我不确定 get_posts,但您检查过 query_posts 吗?

    Template Tags/query posts

    它的工作方式与 get_posts 相同,不确定它是否不那么“昂贵”但试一试

    $catnow = 13; //replace with category id you want to pull 
    $exception = ',-19,-18'; //replace with category ids you want to exclude
    $post_args = array('cat'=>$catnow.$exception);
    query_posts($post_args); 
    if (have_posts()): while (have_posts()): the_post();
    //do something to the posts
    endwhile; 
    else: 
    //no posts found
    endif;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-22
      • 1970-01-01
      • 2014-03-24
      • 2014-07-12
      • 1970-01-01
      • 2012-06-29
      • 1970-01-01
      相关资源
      最近更新 更多