0

i use below code :

//custom post = project
//taxonomy = project-category

$args = array(
    'post_type' => 'project',
);
$query = new WP_Query( $args );

// The Loop
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
         $query->the_post(); ?>

             <div class="element-item col-lg-4 project hover-trigger landscape transition motal" data-category="transition"></div>

             <?php
          }
    wp_reset_postdata();
 }

how to get category`s slug of each post in loop?

Thanks

1

get_the_category() retrieves WP_Term objects for all the categories assigned to the post. You can combine that with wp_list_pluck() to just get the slugs:

$categories = get_the_terms( null, 'project-category' );
$category_slugs = wp_list_pluck( $categories, 'slug' );

$category_slugs will now be an array of slugs for the categories of the post.

  • Thanks . print_r($category_slugs) in loop retrieves array() !! – Mostafa Norzade Sep 1 '18 at 10:27
  • Do your posts have categories? Or a custom taxonomy? – Jacob Peattie Sep 1 '18 at 10:29
  • custom post : project . taxonomy : project-category – Mostafa Norzade Sep 1 '18 at 10:30
  • An important detail that really needed to be in the question. I've updated my answer. – Jacob Peattie Sep 1 '18 at 10:35

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.