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)
部分…因此,我只使用本文中的解决方案,它的工作方式与预期一样。谢谢