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 1
Add a comment  | 

1 Answer 1

Reset to default 0

That 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