$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>custom post types - Output all terms slugs for a loop filter|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin9PV0评论

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

Post a comment

comment list (0)

  1. No comments so far