custom post types - Output all terms slugs for a loop filter

admin2025-06-06  8

I have created a filter on a custom post type loop using terms slugs just like that :

<li><a href="#" data-filter=".<?php echo esc_attr( $cat->slug ); ?>"><?php echo esc_html( $cat->name ); ?> </a></li>

and

<div class="grid-item <?php echo esc_attr( $cat->slug ); ?> js-isotope-item js-grid-item">

But several posts are linked to multiple taxonomy terms and $cat->slug just take the first slug..

Is there any way to call all the slugs and output them with a separator (space) to have different classes ?

Sorry if I'm not clear, english is not my language..

Thank you for help !

I have created a filter on a custom post type loop using terms slugs just like that :

<li><a href="#" data-filter=".<?php echo esc_attr( $cat->slug ); ?>"><?php echo esc_html( $cat->name ); ?> </a></li>

and

<div class="grid-item <?php echo esc_attr( $cat->slug ); ?> js-isotope-item js-grid-item">

But several posts are linked to multiple taxonomy terms and $cat->slug just take the first slug..

Is there any way to call all the slugs and output them with a separator (space) to have different classes ?

Sorry if I'm not clear, english is not my language..

Thank you for help !

Share Improve this question asked Oct 30, 2018 at 18:06 NicautreNicautre 213 bronze badges 1
  • Where do you set the value for $cat? – Milo Commented Oct 31, 2018 at 4:18
Add a comment  | 

1 Answer 1

Reset to default 0

To print or to call all the slugs, you can use the following codes.

<?php 
$cats = get_the_terms(get_the_id(), 'your-taxonomy-id');
   if( is_array($cats) ){
      foreach ( $cats as $cat ) {
           echo $cat->slug.' ';
       }
   }
?>

For more details, follow the below link.
Add custom category name as data-filter to switch between these categories

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749208634a317271.html

最新回复(0)