Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在WP_查询中从多个父项中排除子类别?_Php_Arrays_Wordpress_Foreach - Fatal编程技术网

Php 如何在WP_查询中从多个父项中排除子类别?

Php 如何在WP_查询中从多个父项中排除子类别?,php,arrays,wordpress,foreach,Php,Arrays,Wordpress,Foreach,我有一些密码 <?php $exclude_category = [10]; $company_category_parents = get_categories( array('child_of' => 5) ); foreach ($company_category_parents as $company_category_child) { array_push($exclude_category, $co

我有一些密码

<?php
        $exclude_category = [10];

        $company_category_parents = get_categories( array('child_of' => 5) );
        foreach ($company_category_parents as $company_category_child) {
            array_push($exclude_category, $company_category_child->cat_ID);
        }

        $interviews_category_parents = get_categories( array('child_of' => 58) );
        foreach ($interviews_category_parents as $interviews_category_child) {
            array_push($exclude_category, $interviews_category_child->cat_ID);
        }

        $recent_news_main_query = new WP_Query( array(
            'posts_per_page' => 3,
            'category__not_in' =>  $exclude_category,
        ) );

“是否有任何方法可以使它在超过2个ID的情况下更可用?”-当然,只需添加一个级别的循环即可。将ID放入一个数组中,在该数组上循环,在该循环中,执行您当前为其中一个类别所做的操作,用外部循环中的当前ID替换
数组('child_of'=>5)
部分…因此,我只使用本文中的解决方案,它的工作方式与预期一样。谢谢