Use the_taxonomies() to create a simple list

I’m trying to create <div class="cat-hidden categories"></div> where categories is a list of the categories the current post is posted in.

When just using the_taxonomies(), it outputs something like this:

Read More
<div class="cat-hidden Job Type: &lt;a href='http://www.cirkut.net/wp/libertyguide/genre/early-career/'&gt;Early-Career&lt;/a&gt;, &lt;a href='http://www.cirkut.net/wp/libertyguide/genre/internship/'&gt;Internship&lt;/a&gt;, &lt;a href='http://www.cirkut.net/wp/libertyguide/genre/other/'&gt;Other&lt;/a&gt;, and &lt;a href='http://www.cirkut.net/wp/libertyguide/genre/web-developmentit/'&gt;Web Development/IT&lt;/a&gt;."></div>`

Whereas I want it to output something similar to this:

<div class="cat-hidden Early-Career Internship Web-Development/IT"></div>

That means I’m going to also have to parse the categories that are outputted because I need Web Development/IT to be Web-Development/IT or Web-Development-IT. I’m unsure about the / being in there because I think CSS doesn’t allow a /. That’s where you experts come along.

I am very proficient in HTML and CSS, and know some PHP and WordPress, but I don’t know where to begin.

If any more info is needed, please let me know.

Thank you!

Related posts

Leave a Reply

2 comments

  1. you can use wp_get_post_terms() to get a list of the post categories and just output the category slug which is already phrased for you, something like this:

    //in your loop
    echo '<div class="cat-hidden';
    $cats = wp_get_post_terms($post->ID,'category');
    foreach($cats as $cat){
       echo ' '.$cat->slug;
    }
    echo '"></div>';
    
  2. Just to offer up some different approaches …

    $terms = get_the_terms( $post->ID, 'category' );
    $terms = ( $terms ) ? wp_list_pluck( $terms, 'slug' ) : array();
    $space = !empty( $terms ) ? ' ' : '';
    printf( '<div class="cat-hidden%s"></div>', $space . implode( ' ', $terms ) );
    

    Another …

    $terms = get_the_terms( $post->ID, 'category' );
    $terms = ( $terms ) ? wp_list_pluck( $terms, 'slug' ) : array();
    $classes = !empty( $terms ) ? ' ' . implode( ' ', $terms ) : '';
    echo "<div class='cat-hidden$classes'></div>";
    

    That wp_list_pluck function is real handy for these kind of things, avoids the need to do any looping yourself..