0

I am trying to display the categories name on my page but it's not displaying. I am using the below code and I added the shortcode gridCategories on my page. I am getting only array

function createGridCategories(){
$categories = get_categories( array(
    'taxonomy'   => 'category',
    'orderby'    => 'name',
    'parent'     => 0,
    'hide_empty' => 0, // change to 1 to hide categores not having a single post
) );
var_dump($categories);
return $categories;
}
add_shortcode( 'gridCategories', 'createGridCategories');

I tried this also

$categories = get_the_category();
var_dump($categories);

I added a shortcode like this in textblock

enter image description here But still, I am not getting any output on my page. Is there any issue with my code?

1 Answer 1

1

Is there any issue with my code?

Yes, there is. get_categories() returns an array of terms, e.g. WP_Term objects or a list of term IDs, so you can't simply do return $categories;.

Instead,

  • For a basic list such as Foo category, Bar category, etc. (i.e. no category links), you can simply set the fields parameter to names which then gives you a list of category names:

    function createGridCategories() {
        $categories = get_categories( array(
            'fields'     => 'names',
            'hide_empty' => 0,
            // other args here
        ) );
    
        return implode( ', ', $categories );
    }
    
  • Or you can manually loop through the terms and just build the markup/HTML to your own liking:

    function createGridCategories() {
        $categories = get_categories( array(
            'hide_empty' => 0,
            // other args here
        ) );
    
        $list = '';
    
        foreach ( $categories as $term ) {
            $url = get_category_link( $term );
            $list .= '<li><a href="' . esc_url( $url ) . '">' . esc_html( $term->name ) . '</a></li>';
        }
    
        return "<ul>$list</ul>";
    }
    
  • Or alternatively (for a list/UL like the above), you can use wp_list_categories():

    function createGridCategories() {
        $list = wp_list_categories( array(
            'taxonomy'   => 'category',
            'hide_empty' => 0,
            'echo'       => 0,
            'title_li'   => '',
            // other args here
        ) );
    
        return "<ul>$list</ul>";
    }
    
11
  • 1
    Yes, It's working perfectly. Thank you so much. Appreciate for giving me a valuable information. Upvote from my side. Aug 9, 2020 at 12:56
  • I tried all three options. All are working. I need a small help in the second one. How can I use UL LI ? Aug 9, 2020 at 14:04
  • Just do $list[] = '<li><a ...>...</a></li>' and return "<ul>$list</ul>" like in the revised code.
    – Sally CJ
    Aug 9, 2020 at 14:10
  • Yes,It's working, I also remove the comma from implode. Aug 9, 2020 at 14:18
  • 1
    Glad I could help :), and I've changed the $list in the second code to a string as I mentioned before. Cheers.
    – Sally CJ
    Aug 11, 2020 at 2:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.