admin管理员组文章数量:1318773
I would like to show a list of categories that has posts with a given custom taxonomy.
And then the link to each category archive should include that custom taxonomy at the end like this: mysite/category/catname/?taxonomy=taxname
It should be a custom function that I have no idea how to create that I can use in my theme file but I have this slightly modified code from another answer () as a starting point:
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;
}
Then, I try to use that function like this:
echo double_term_tree( 'post', 'taxname', 'catname' );
But the function does not output anything..
I would like to show a list of categories that has posts with a given custom taxonomy.
And then the link to each category archive should include that custom taxonomy at the end like this: mysite/category/catname/?taxonomy=taxname
It should be a custom function that I have no idea how to create that I can use in my theme file but I have this slightly modified code from another answer (https://wordpress.stackexchange/a/97926/195913) as a starting point:
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;
}
Then, I try to use that function like this:
echo double_term_tree( 'post', 'taxname', 'catname' );
But the function does not output anything..
Share Improve this question asked Oct 11, 2020 at 10:39 AleksanderKanelAleksanderKanel 11 Answer
Reset to default 0That is a reserve keyword, it should changed into another keyword like post_tag
You may also declare a custom taxonomy using a plugin
or in function.php
but most developers do it using a plugin for faster development. it is not a heavy plugin though
because of that wrong keyword. the result is not showing.
本文标签: plugin developmentShow list of categories that has posts with different taxonomies
版权声明:本文标题:plugin development - Show list of categories that has posts with different taxonomies 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742047744a2417888.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论