很抱歉,这篇自定义帖子和分类法内容太新了。我需要帮助来显示按分类法排列的自定义帖子类型。
输出如下:
父1分类标题子1分类标题子2分类标题子1分类标题子2分类标题子3分类标题子1分类标题子分类法2下的文章标题父分类法1下的文章标题子分类法1下的文章标题子分类法2下的文章标题当前我正在使用此代码
$custom_terms = get_terms(\'material_category\');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array(\'post_type\' => \'materials\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'material_category\',
\'field\' => \'slug\',
\'terms\' => array(\'general-english\')
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo \'<h2>\'.$custom_term->name.\'</h2>\';
while($loop->have_posts()) : $loop->the_post();
echo \'<a href="\'.get_permalink().\'">\'.get_the_title().\'</a>\';
endwhile;
}
但它单独显示父级。提前感谢您的帮助:)
最合适的回答,由SO网友:Tunji 整理而成
这是因为您在循环外重复父术语名称。
$custom_terms = get_terms(\'material_category\');
foreach($custom_terms as $custom_term) {
$args = array(\'post_type\' => \'materials\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'material_category\',
\'field\' => \'slug\',
\'terms\' => array(\'general-english\')
),
),
);
$loop = new WP_Query($args);
if ($loop->have_posts()) {
while ($loop->have_posts()) : $loop->the_post();
echo \'<h2>\' . $custom_term->name . \'</h2>\';
echo \'<a href="\' . get_permalink() . \'">\' . get_the_title() . \'</a>\';
endwhile;
}
wp_reset_query();
}