0

I'm trying to output slugs as class names for all of the taxonomies that a post is attached to, so I can filter those posts using MixItUp3 with the Multifier Extension on a custom directory page.

I have a custom loop setup that only pulls through directory posts that have been tagged 'current' (this allows old listings to remain in the system but not be displayed).

<?php 

$args = array(
    'post_type' => 'directory',
    'tax_query' => array(
    'relation' => 'AND',
        array(
            'taxonomy' => 'directory_cat',
            'operator' => 'EXISTS'
        ),
        array(
            'taxonomy' => 'directory_tag',
            'field' => 'slug',
            'terms' => 'current'
        )
    )
);

$query = new WP_Query( $args ); if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();

?>

<div class="column mix<?php if ($class_names) { echo ' ' . $class_names;} ?>">
    <a href="<?php the_permalink(); ?>">
        ...
    </a>
</div>

I'm trying to output the slugs into the div class as shown above ($class_names), but I can't find the right way to do it. The following code was taken from the MixItUp documentation and I've tried to edit it to my needs but as a php beginner I just keep faltering.

$taxonomies = get_terms();
$slugs = wp_list_pluck($taxonomies, 'slug');
$class_names = join(' ', $slugs);

It outputs and formats correctly but it lists every taxonomy in the system, rather than the ones attached to each post in the loop.

0

I'd recommend using get_the_terms instead of get_terms. It specifically pulls out the terms that are attached to a given post. If you use it within the loop, it'll only give you the terms that are attached to that post.

It'd look something like this:

$query = new WP_Query( $args ); if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();

$slugs = array();    
$directory_terms = get_the_terms( $post->ID, 'directory_tag');

foreach ( $directory_terms as $term ) {
    $slugs[] = $term->slug;
}

$class_names = join( ' ', $slugs ); 

?> 

<div class="' . <?php if ( isset( $class_names) ) { echo ' ' . $class_names;} ?> . '"> 

You could obviously refine your search further if you only need specific terms from that taxonomy. get_the_terms returns a list of WP_Term objects that you can narrow down in any way you see fit.

EDIT: Updated code to show how to implement in a div.

EDIT: Added missing semi-colon.

  • Thanks for your answer Mario, I've tried to replicate what you've posted into my code but I'm still struggling as my php is not so strong, sorry. Any chance you can offer me an example of actually outputting the code into the div? Cheers – Daniel Watts Feb 13 '17 at 0:52
  • Well, at this point you'd have all your slugs in an array, so you could apply them in the same way as in your example. I'll edit my code block accordingly – Mario Vega Feb 13 '17 at 3:19
  • Thanks Mario, got it. There's still further for me to delve with this but appreciate you helping me get past this. Just going to make one edit to your code as there's a missing semi colon in the foreach that was causing errors. Cheers – Daniel Watts Feb 13 '17 at 4:16
  • Drat! Those semi-colons will be the death of me... Good luck with everything! – Mario Vega Feb 13 '17 at 4:56

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.