I am working on a WordPress theme and this is my code to display posts.

However I'm struggling to display all categories for a post. Most solutions don't work for me but do for others. That's also the reason why I post my full code on here because this code may not be the best one to use.

Thanks in advance!

$wpb_all_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>-1));

if ( $wpb_all_query->have_posts() ) :
    while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post();
            <article id="post-5" class="slide taken" data-background-l="dark" data-background-p="dark" itemscope itemtype="http://schema.org/CreativeWork">
                <header class="page-header caption">
                        <h2 class="title" itemprop="name"><?php the_title(); ?></h2>
                        <h6 class="subtitle"><?php echo $cats[0]->name; ?></h6>
                        <div class="excerpt" itemprop="description"><p><?php the_excerpt(); ?></p></div>
                        <a class="mitch-button to-load dark" href="<?php the_permalink(); ?>" data-i="1" data-title="Lapka"><span>View Post<svg xmlns="http://www.w3.org/2000/svg"  x="0" y="0" width="24" height="42" viewBox="0 0 24 42" enable-background="new 0 0 24 42" xml:space="preserve"><polygon fill="dark" points="23.3 20.1 23.3 20.1 23.3 20.1 20.1 23.3 20.1 23.3 3.1 40.3 0 37.1 17 20.1 0 3.1 3.1 0 20.1 17 20.1 17 "/></svg></span></a>
                <div class="media" data-bg-small="/wp-content/uploads/2015/11/1-1-1920x1071.jpg" data-bg-full="/wp-content/uploads/2015/11/1-1.jpg" data-bg-small-p="/wp-content/uploads/2015/11/1_vertical-855x1140.jpg" data-bg-large-p="/wp-content/uploads/2015/11/1_vertical-1110x1480.jpg" data-bg-full-p="/wp-content/uploads/2015/11/1_vertical.jpg">
                    <img src="/wp-content/uploads/2015/11/1-1-960x535.jpg" alt="Lapka" data-lazyload="innoway" itemprop="image" srcset="data:image/gif;base64,R0lGODlhAQABAJH/AP///wAAAMDAwAAAACH5BAEAAAIALAAAAAABAAEAAAICVAEAOw==" />

else :
    _e( 'Sorry, no posts matched your criteria.' );
endif; ?>
  • There are many usefull answers on-site on how to use get_the_category() and related functions to display a list of post categories – Pieter Goosen Jul 20 '16 at 10:59

The below code check that post have category or not. If post have category then it will display list of category separate by comma. You can try this code. Hope this will help you.

   echo $cats_list = get_the_category_list(', ');

  • Thanks, that's great. Is there any way to remove the link from the cats? – J. Doe Jul 20 '16 at 12:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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