admin管理员组

文章数量:1287597

I am using the snippet below to create a shortcode to get all tags for a search in a separate list including tag count. Eg search for 'foo' and it returns all tags for all posts that match the search 'foo'.

I am trying to also get the categories from the same search in a separate list using a second shortcode from the same global search. I am not sure how I can get the categories from the same global search.

Eg search for for 'foo' and get:

Tag: Tag1 (10), Tag2 (20), Tag3 (2) etc (this is the shortcode and already works)

Category: Cat1 (10), Cat2 (2) etc. (not sure how to get the second shortcode to work)

    function get_query_terms_list( $taxonomy = 'post_tag', $sep = '' ) {
    $list = array();

    foreach ( $GLOBALS['wp_query']->posts as $post ) {
        if ( is_array( $terms = get_the_terms( $post, $taxonomy ) ) ) {
            foreach ( $terms as $term ) {
                // Set the term's initial count.
                if ( ! isset( $list[ $term->term_id ] ) ) {
                    $list[ $term->term_id ] = 0;
                }
                // And then increment it for each post.
                $list[ $term->term_id ]++;
            }
        }
    }

    // Sort by the count, highest to lowest.
    arsort( $list );

    $links = array();

    foreach ( $list as $term_id => $count ) {
        $term = get_term( $term_id );
        $link = get_term_link( $term );

        if ( ! is_wp_error( $link ) ) {
            $links[] = '<a style="background-color:#fffff; border-radius:20px; box-shadow: 0 3px 5px 0 rgb(0 0 0 / 8%); font-weight: bold; color:#6c757d; padding:8px; margin:5px;" href="'.$link.'">' . __( $term->name ) ." ($count)". '</a>'.'';
            
        }
    }

    return '<div style="height:50px; padding-top:10px; overflow-x:auto; white-space: nowrap;">'.implode( $sep, $links ).'</div>';
}

Shortcode

add_shortcode( 'bd_terms_list', 'bd_terms_list_custom_callback');
function bd_terms_list_custom_callback( $args ){
    return get_query_terms_list( 'gd_place_tags' );
}

I am using the snippet below to create a shortcode to get all tags for a search in a separate list including tag count. Eg search for 'foo' and it returns all tags for all posts that match the search 'foo'.

I am trying to also get the categories from the same search in a separate list using a second shortcode from the same global search. I am not sure how I can get the categories from the same global search.

Eg search for for 'foo' and get:

Tag: Tag1 (10), Tag2 (20), Tag3 (2) etc (this is the shortcode and already works)

Category: Cat1 (10), Cat2 (2) etc. (not sure how to get the second shortcode to work)

    function get_query_terms_list( $taxonomy = 'post_tag', $sep = '' ) {
    $list = array();

    foreach ( $GLOBALS['wp_query']->posts as $post ) {
        if ( is_array( $terms = get_the_terms( $post, $taxonomy ) ) ) {
            foreach ( $terms as $term ) {
                // Set the term's initial count.
                if ( ! isset( $list[ $term->term_id ] ) ) {
                    $list[ $term->term_id ] = 0;
                }
                // And then increment it for each post.
                $list[ $term->term_id ]++;
            }
        }
    }

    // Sort by the count, highest to lowest.
    arsort( $list );

    $links = array();

    foreach ( $list as $term_id => $count ) {
        $term = get_term( $term_id );
        $link = get_term_link( $term );

        if ( ! is_wp_error( $link ) ) {
            $links[] = '<a style="background-color:#fffff; border-radius:20px; box-shadow: 0 3px 5px 0 rgb(0 0 0 / 8%); font-weight: bold; color:#6c757d; padding:8px; margin:5px;" href="'.$link.'">' . __( $term->name ) ." ($count)". '</a>'.'';
            
        }
    }

    return '<div style="height:50px; padding-top:10px; overflow-x:auto; white-space: nowrap;">'.implode( $sep, $links ).'</div>';
}

Shortcode

add_shortcode( 'bd_terms_list', 'bd_terms_list_custom_callback');
function bd_terms_list_custom_callback( $args ){
    return get_query_terms_list( 'gd_place_tags' );
}
Share Improve this question asked Sep 19, 2021 at 6:24 RobHRobH 73 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

how I can get the categories from the same global search

You can add a taxonomy attribute to your shortcode and then pass the value to get_query_terms_list(), like so:

add_shortcode( 'bd_terms_list', 'bd_terms_list_custom_callback');
function bd_terms_list_custom_callback( $args ){
    $atts = shortcode_atts( array( // list of default attributes
        'taxonomy' => 'gd_place_tags',
        'sep'      => '', // optional, but you might use it someday? :)
    ), $args );

    return get_query_terms_list( $atts['taxonomy'], $atts['sep'] );
}

See the Shortcodes API on Codex for more details about handling shortcode attributes.

And for example with the default post_tag and category taxonomies included in WordPress core, you can display the tags and categories in separate list like so:

Tags: [bd_terms_list taxonomy="post_tag"]

Categories: [bd_terms_list taxonomy="category"]

So you really just need to use the correct taxonomy slug, just like you could see above.

In response to your comment:

the slug for category is 'gd_postcategory' without the underscore for some reason. I'm not sure how to fix the underscore, so I added 2 x shortcodes now instead of using the more elegant taxonomy="category". It works :)

Why would you need to "fix the underscore"?

And you do know the differences between taxonomies and terms, don't you? (see https://developer.wordpress/themes/basics/categories-tags-custom-taxonomies/)

So for example, if I registered custom "tag" and "category" taxonomies named gd_place_tags and gd_postcategory respectively, then I would display the tag/category list like so:

Tags: [bd_terms_list taxonomy="gd_place_tags"]

Categories: [bd_terms_list taxonomy="gd_postcategory"]

But if you're absolutely certain the above just doesn't work for you, then yes of course, you could just create another shortcode which calls the get_query_terms_list() function. :)

However, if I were you, I would try again with the modified bd_terms_list_custom_callback() function as well as the HTML code above, but I would double-check that I'm using the correct taxonomy slugs (and not the label like "My Taxonomy" or even the slug of a term in the taxonomy like my-category-in-my-taxonomy).

本文标签: shortcodeReturn all Tags and Categories in Separate List