I am trying to retrieve all posts to be displayed on a page. And i plan to use this W3 Resource filter mechanism (javascript in snippet plugin). I already have the category names for each post.

For the filter to work I need the category name in the class of an element.

So far I have this to retrieve all the post:

<?php $args = array (
      'posts_per_page' => '-1',
      'post_type' => 'works'
      $work_list = new WP_Query($args);
<?php if( $work_list->have_posts() ):?>
<?php while( $work_list->have_posts() ) : $work_list->the_post();?>
     echo ' <div id="work-post">
        <a href="'. get_permalink($post->ID) .'">
            <div id="work-thumbnail">
                <img src="' . get_field("work_image") . '" alt="Avatar" class="image">
 <?php endwhile;?>
 <?php endif; ?>
 <?php wp_reset_query();     // Restore global post data stomped by the_post(). ?> 

but how do i get the category for each post and set it as part of its class? also if a post has 2 categories.

i've seen this being used but I'm not sure how to make it work for me.

foreach(get_the_category as $category) {

  • You can get category for certain post using this. $post_id = get_the_ID(); // or use the post id if you already have it $category_object = get_the_category($post_id); foreach($category_object as $cate) { echo $cate->name; } – Vishwa Apr 26 at 7:41

Your Answer

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

Browse other questions tagged or ask your own question.