在我开始之前请注意,你不应该称呼你的帖子类型menu
, menu
是保留术语,因此您可能会遇到默认内置功能的问题。但是,您可以使用复数形式menus
不必担心引发冲突的问题
至于您的代码,您使用的是无效的参数,它只是被WP_Query
这也正是你无论如何都要回所有帖子的原因。对于自定义分类法,应使用tax_query
查询属于自定义分类术语的帖子
以下操作应该有效:(注意:您只需要输入正确的分类名称和正确的术语ID 到tax_query
针对您的特定用例
$posts = get_posts(array(
\'post_type\' => \'menu\',
\'orderby\' => \'name\',
\'order\' => \'DESC\',
\'tax_query\' => array(
array(
\'taxonomy\' => $wcatTerm->taxonomy,
\'terms\' => $wcatTerm->term_id,
\'include_children\' => false,
)
),
\'nopaging\' => true,
));
编辑类别参数将
not 在不使用内置分类法的情况下工作,
category
. 您使用的是自定义分类法,因此要从特定术语或自定义分类法中的术语查询帖子,您需要使用
tax_query
就像我的示例代码一样。
正如我所说的,你需要给correct 您的tax_query
因为我不知道你的确切设置。这个terms
参数(默认情况下)应该是一个整数值或术语ID的整数值数组,所以要考虑到这一点
让我们以代码为例
$wsubcats = get_categories($wsubargs);
foreach ($wsubcats as $wsc):
在这里面
foreach
回路,您的
tax_query
数值如下:
您的taxonomy
值将为$wsc->taxonomy
您的terms
值将为(int) $wsc->term_id
编辑2
筛选代码时,会出现大量错误。我已经更正了您代码中的所有错误,并在代码中对它们发表了评论,所以请务必检查我的评论。在我声明要删除的地方,请删除该特定代码行,因为它不是必需的。此外,我不知道您是否真的需要空术语,所以我保留了它,只需内置保护,以避免在实际存在空术语时出现错误和错误
<?php
$taxonomy = \'menu-items\'; // Do not use hypens in taxonomy names, only use underscores
$term_args = array(
\'hide_empty\' => 0, // Why do you need empty terms?
\'orderby\' => \'ASC\',
\'parent\' => 0
);
$wcatTerms = get_terms(
$taxonomy, // Taxonomy name
$term_args // Array of arguments to get terms by
);
// To avoid bugs and errors, run this only if we have a valid array of terms
if ( !empty( $wcatTerms ) // Check that we do not have an empty array
&& !is_wp_error( $wcatTerms ) // Check that we do not have a WP_Error object returned
) {
// If our conditional check passed, then only run the following code
foreach($wcatTerms as $wcatTerm) { // Do not use : as it is hard to debug and to read, use old style curlies
//echo $wcatTerm->name;
$wsubargs = array(
//\'hierarchical\'=>1, // This is default, not needed and can be removed
//\'show_option_none\'=>\'\', // Invalid parameter, where do you get this from, to remove
\'hide_empty\'=> 0, // Why do you need empty terms?
\'parent\'=>$wcatTerm->term_id,
);
//$wsubcats = get_categories($wsubargs); // Why use get_categories, get_categories uses get_terms, to remove
$wsubcats = get_terms(
$taxonomy, // We have set this earlier
$wsubargs
);
foreach ( $wsubcats as $wsc ) { // Again, do not use : as it is hard to debug
?><h2><?php //echo $wsc->name; ?></h2>
<?php $childcat = $wsc->name; ?>
<h2><?php echo $childcat; ?></h2>
<?php
//$posts = get_posts(array( // Do not mess with $posts global, rather use $posts_array, to remove
$args = array(
\'post_type\' => \'menu\', // Really bad name, reserved name in Wordpress
\'orderby\' => \'name\',
\'order\' => \'DESC\',
\'tax_query\' => array(
array(
\'taxonomy\' => $taxonomy, // We have defined this already right at the start
\'terms => $wsc->term_id,
\'include_children\' => false,
)
),
\'nopaging\' => true,
);
$posts_array = get_posts( $args );
// You are missing the loop here, that is why you are getting the same stuff over and over again
if ( $posts_array ) { // Only run this if we actually have posts, if we do not have posts, we will have bugs and errors
foreach ( $posts_array as $post ) { // You were missing this
setup_postdata( $post ); // VERY VERY IMPORTANT, without this, the template tags like the_content will not work
?>
<div class="menu-title"><?php the_title(); ?></div>
<div class="menu-title"><?php the_content(); ?></div>
<?php } // endforeach $posts_array
wp_reset_postdata(); // VERY IMPORTANT to reset the $post global to main query
} // endif $posts_array
} // endforeach $wsubcats
} // endforeach $wcatTerms
} // endif check for valid array of terms
?>