duanfen7676 2014-11-17 11:59
浏览 65

当参数作为变量时,Wordpress类别_not_in不起作用

This is my category.php. I want to filter some posts from this category. Posts were posted in parent and child categories. Show only posts from this category and not from child categries. "category__not_in" not working when argument given as variable. If I use 'category__not_in'=> array(20,22,23,24) like this it works perfectly.. In below code 'category__not_in'=> array($sub_cat_ids) not works. please help

<?php
$current_cat_id = get_cat_id( single_cat_title("",false) );
$catlist = get_categories('hide_empty=0&child_of='.$current_cat_id.'&depth=10&parent='.$current_cat_id );  
$sub_cat_ids = "";
foreach($catlist as $cat){
   $sub_cat_ids = $cat->cat_ID.",".$sub_cat_ids;
}
if(!empty($catlist)){ ?>
    <ul class="insight_style">
    <?php
        $i=1;
        $args = array(
        'post_type' => 'post',
        'posts_per_page' => -1,
        'category__not_in'=> array($sub_cat_ids),// not working
        'category__in' => $current_cat_id,
        'paged' => $paged
         );
        $page_query = new WP_Query($args); 
        if ( $page_query->have_posts() ) : while ($page_query->have_posts()) : $page_query->the_post(); 

            } ?>            
            <li>
            // do some stuff
            </li>
            <?php  $i++; endwhile; endif; wp_reset_query(); ?>
        </ul>
    <?php  } ?>
  • 写回答

1条回答 默认 最新

  • doudiandi6967 2014-11-17 12:04
    关注

    Let's collect the categories into an array:

    foreach ($catlist as $cat) {
        $catIds[] = $cat->cat_ID;
    }
    

    And later join them:

    'category__not_in'=> array(join(",",$catIds)),
    

    But first of all, var_dump($catlist); to see, is there any category in that.

    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探