• As part of the theme I am using, at the end of each post it displays a list of the categories that have been applyed to the post.

    I would also like to display a list of all the categories (not just the ones for that post) used by the site.

    I have tried using <?php wp_list_cats(‘arguments’); ?> but it has printed them in a vertical list.

    You can see it here on this post:
    https://electriccoolbox.org.uk/campingaz-powerbox-36-litre-electric-cool-box/

    This is the code, the first two work as I want but the third does not:
    <p class=”postmetadata”><?php if (function_exists(‘the_tags’)) { the_tags(‘View all items from ‘, ‘, ‘, ‘ ‘); } ?> | View all <?php the_category(‘, ‘) ?> | View all <?php wp_list_cats(‘arguments’); ?></p>

    Any ideas?

    Thanks.

Viewing 1 replies (of 1 total)
  • Joe,
    You can give this code a try. I know there’s a lot but I just had cup of coffee and can’t stop typing ?? Should work in any template file you place it in. The best part is you can replace the first argument passed to get_terms() to any taxonomy you wish displayed ie. ‘post-tags’, ‘categories’ or ‘my-custom-taxonomy’.

    Best wishes,
    -Mike

    $category_links = array();
    $categories = get_terms( 'category' );
    if( !is_wp_error( $categories ) ) {
    	foreach( $categories as $order => $category ) {
    		$url = esc_url( get_category_link( $category->term_id ) );
    		$title = esc_attr( $category->name );
    		$text = esc_html( $category->name );
    		$category_links[] = "\n" . '<a href="' . $url . '" title="' . $title . '" >' . $text . '</a>';
    	}
    }
    $category_links_count = count( $category_links );
    if( $category_links_count > 1 )
    	print 'Categories: ' . implode( ', ', $category_links );
    else if( $category_links_count === 1 )
    	print 'Category: ' . $category_links_count[0];
Viewing 1 replies (of 1 total)
  • The topic ‘Display horizontal list of all categories’ is closed to new replies.