我想显示具有给定自定义分类法的帖子的类别列表。
然后,到每个类别归档的链接应该在末尾包括这样的自定义分类法:mysite。com/类别/catname/?分类法=分类名称
这应该是一个自定义函数,我不知道如何创建它,我可以在我的主题文件中使用,但我从另一个答案中得到了这个稍微修改的代码(https://wordpress.stackexchange.com/a/97926/195913) 作为起点:
function double_term_tree(
$post_types = \'post\',
$first = \'category\',
$second = \'taxonomy\'
)
{
$query = new WP_Query(
array (
\'numberposts\' => -1,
\'suppress_filters\' => TRUE,
\'posts_per_page\' => -1,
\'post_type\' => $post_types,
\'tax_query\' => array (
\'relation\' => \'AND\',
array(
\'taxonomy\' => $first,
\'field\' => \'id\',
\'terms\' => get_terms( $first, array( \'fields\' => \'ids\' ) )
),
array(
\'taxonomy\' => $second,
\'field\' => \'id\',
\'terms\' => get_terms( $second, array( \'fields\' => \'ids\' ) )
),
),
)
);
if ( empty ( $query->posts ) )
return;
$result_list = array();
$output = \'<ul>\';
foreach ( $query->posts as $post )
{
$first_terms = get_the_term_list( $post->ID, $first, \'\', \'|\' );
$second_terms = get_the_term_list( $post->ID, $second, \'\', \'|\' );
$f_term_array = explode( \'|\', $first_terms );
$s_term_array = explode( \'|\', $second_terms );
foreach ( $f_term_array as $f_term )
{
if ( ! isset ( $result_list[ $f_term ] ) )
$result_list[ $f_term ] = array();
$result_list[ $f_term ] = array_merge( $result_list[ $f_term ], $s_term_array );
}
}
foreach ( $result_list as $k => $v )
{
$result_list[ $k ] = array_unique( $v );
$output .= "\\n<li>$k\\n\\t<ul>\\n\\t\\t<li>"
. join( "</li>\\n\\t\\t<li>", array_unique( $v ) )
. "</li>\\n\\t</ul>\\n</li>";
}
$output .= \'</ul>\';
return $output;
}
然后,我尝试这样使用该函数:
echo double_term_tree( \'post\', \'taxname\', \'catname\' );
但函数不输出任何内容。。