Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I thought this would be easy enough, although it's proving quite difficult. My end goal is to integrate jQuery isotope into my wordpress portfolio. I've gotten the isotope to work outside of wordpress, but I am having a very difficult time assigning my custom taxonomies as class names. So I don't need help with isotope, just assigning taxonomies as classes.

I have a custom post type portfolio.

The portfolio has 2 custom taxonomies that I want to use to filter my results on an archive page. One taxonomy is "media" the other is "campaigns"

So if I assign a media taxonomy of "print" and a campaign taxonomy of "local" to a post from portfolio, I'd like the output on the archive page to be something like this:

<div id="post-34" class="print local">...</div>

However I currently have this

<div id="post-34" class>...</div>

i followed instructions from the codex on get_the_terms. I added this code to my functions.php file:

<?php // get taxonomies terms links
function custom_taxonomies_terms_links() {
    global $post, $post_id;
    // get post by post id
    $post = &get_post($post->ID);
// get post type by post
    $post_type = $post->post_type;
// get post type taxonomies
    $taxonomies = get_object_taxonomies($post_type);
    foreach ($taxonomies as $taxonomy) {
        // get the terms related to post
        $terms = get_the_terms( $post->ID, $taxonomy );
        if ( !empty( $terms ) ) {
            $out = array();
            foreach ( $terms as $term )
                $out[] = '<a href="' .get_term_link($term->slug, $taxonomy) .'">'.$term->name.'</a>';
        $return = join( ', ', $out );
    }
}
return $return;
} ?>

Then I dropped in the echo call into the class call in my loop on the archive-portfolio.php page like this:

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

        <div id="post-<?php the_ID(); ?>" class="<?php echo custom_taxonomies_terms_links(); ?>">

Any help would be greatly appreciated. This is driving me nuts that I can't figure this out.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

The function you mention is going to return a list of links -- aren't you just trying to get the term slug as a class? Why not do....

$terms = get_the_terms($post->ID, 'my-category');
$more_terms = get_the_terms($post->ID, 'another-category');

$slug_out = array();

foreach($terms as $term){
    $slug_out[] = $term->slug;
}

foreach($more_terms as $term){
    $slug_out[] = $term->slug;
}

echo '<div class="'.implode(' ', $slug_out).'">';
share|improve this answer
    
Thanks so much! This totally works and makes a lot of sense. One question though, Is there any way to make it work for two taxonomies? So in your example a 'my-category' and 'my-category-two'? Thanks again, really appreciate it! –  Ryan Swanson Aug 6 '13 at 1:58
    
yea. updated answer –  GhostToast Aug 6 '13 at 17:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.