Skip to main content
deleted 1 character in body
Source Link
Sally CJ
  • 39.9k
  • 2
  • 26
  • 48

Note that the get_the_category() documentation stated that:

Note: This function only returns results from the default "category" taxonomy. For custom taxonomies use get_the_terms().

So if I guessed it correctly that you're trying to display the blogs_cats terms for the current post, then you should use get_the_terms() instead.

There's also get_the_term_list() if you just want to display a list of terms like Foo Bar, Term 2 where each term links to their own archive page.

Examples:

  1. Manually loop through the terms returned by get_the_categoryget_the_terms() to output a simple list of term names only(only):

    $category_detail = get_the_terms( $tid, 'blogs_cats' );
    
    $cats = array();
    if ( is_array( $category_detail ) ) {
        foreach ( $category_detail as $cd ) {
            $cats[] = $cd->name;
        }
    }
    
    $data .= "<li>blogs_cats for $tid: " . implode( ', ', $cats ) . '</li>';
    
  2. Or use get_the_term_list() to output a list of term names + links:

    $cat_list = get_the_term_list( $tid, 'blogs_cats', '<i>', ', ', '</i>' );
    $data .= "<li>blogs_cats for $tid: $cat_list</li>";
    

Additionally, you should call shortcode_atts() to ensure that the cat argument exists in $atts (e.g. when using just [blog], i.e. without specifying any parameters). E.g.

function blogView( $atts ){
    global $post;

    $atts = shortcode_atts( array(
        'cat' => 'All',
    ), $atts );

    // ... your code.
}

Note that the get_the_category() documentation stated that:

Note: This function only returns results from the default "category" taxonomy. For custom taxonomies use get_the_terms().

So if I guessed it correctly that you're trying to display the blogs_cats terms for the current post, then you should use get_the_terms() instead.

There's also get_the_term_list() if you just want to display a list of terms like Foo Bar, Term 2 where each term links to their own archive page.

Examples:

  1. Manually loop through the terms returned by get_the_category() to output a simple list of term names only:

    $category_detail = get_the_terms( $tid, 'blogs_cats' );
    
    $cats = array();
    if ( is_array( $category_detail ) ) {
        foreach ( $category_detail as $cd ) {
            $cats[] = $cd->name;
        }
    }
    
    $data .= "<li>blogs_cats for $tid: " . implode( ', ', $cats ) . '</li>';
    
  2. Or use get_the_term_list() to output a list of term names + links:

    $cat_list = get_the_term_list( $tid, 'blogs_cats', '<i>', ', ', '</i>' );
    $data .= "<li>blogs_cats for $tid: $cat_list</li>";
    

Additionally, you should call shortcode_atts() to ensure that the cat argument exists in $atts (e.g. when using just [blog], i.e. without specifying any parameters). E.g.

function blogView( $atts ){
    global $post;

    $atts = shortcode_atts( array(
        'cat' => 'All',
    ), $atts );

    // ... your code.
}

Note that the get_the_category() documentation stated that:

Note: This function only returns results from the default "category" taxonomy. For custom taxonomies use get_the_terms().

So if I guessed it correctly that you're trying to display the blogs_cats terms for the current post, then you should use get_the_terms() instead.

There's also get_the_term_list() if you just want to display a list of terms like Foo Bar, Term 2 where each term links to their own archive page.

Examples:

  1. Manually loop through the terms returned by get_the_terms() to output a simple list of term names (only):

    $category_detail = get_the_terms( $tid, 'blogs_cats' );
    
    $cats = array();
    if ( is_array( $category_detail ) ) {
        foreach ( $category_detail as $cd ) {
            $cats[] = $cd->name;
        }
    }
    
    $data .= "<li>blogs_cats for $tid: " . implode( ', ', $cats ) . '</li>';
    
  2. Or use get_the_term_list() to output a list of term names + links:

    $cat_list = get_the_term_list( $tid, 'blogs_cats', '<i>', ', ', '</i>' );
    $data .= "<li>blogs_cats for $tid: $cat_list</li>";
    

Additionally, you should call shortcode_atts() to ensure that the cat argument exists in $atts (e.g. when using just [blog], i.e. without specifying any parameters). E.g.

function blogView( $atts ){
    global $post;

    $atts = shortcode_atts( array(
        'cat' => 'All',
    ), $atts );

    // ... your code.
}
Source Link
Sally CJ
  • 39.9k
  • 2
  • 26
  • 48

Note that the get_the_category() documentation stated that:

Note: This function only returns results from the default "category" taxonomy. For custom taxonomies use get_the_terms().

So if I guessed it correctly that you're trying to display the blogs_cats terms for the current post, then you should use get_the_terms() instead.

There's also get_the_term_list() if you just want to display a list of terms like Foo Bar, Term 2 where each term links to their own archive page.

Examples:

  1. Manually loop through the terms returned by get_the_category() to output a simple list of term names only:

    $category_detail = get_the_terms( $tid, 'blogs_cats' );
    
    $cats = array();
    if ( is_array( $category_detail ) ) {
        foreach ( $category_detail as $cd ) {
            $cats[] = $cd->name;
        }
    }
    
    $data .= "<li>blogs_cats for $tid: " . implode( ', ', $cats ) . '</li>';
    
  2. Or use get_the_term_list() to output a list of term names + links:

    $cat_list = get_the_term_list( $tid, 'blogs_cats', '<i>', ', ', '</i>' );
    $data .= "<li>blogs_cats for $tid: $cat_list</li>";
    

Additionally, you should call shortcode_atts() to ensure that the cat argument exists in $atts (e.g. when using just [blog], i.e. without specifying any parameters). E.g.

function blogView( $atts ){
    global $post;

    $atts = shortcode_atts( array(
        'cat' => 'All',
    ), $atts );

    // ... your code.
}