Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd custom taxonomies as class names to an archive page of custom post types, wordpress
    primarykey
    data
    text
    <p>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.</p> <p>I have a custom post type of portfolio</p> <p>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"</p> <p>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:</p> <pre><code>&lt;div id="post-34" class="print local"&gt;...&lt;/div&gt; </code></pre> <p>However I currently have this</p> <pre><code>&lt;div id="post-34" class&gt;...&lt;/div&gt; </code></pre> <p>i followed instructions from the codex on get_the_terms. I added this code to my functions.php file:</p> <pre><code>&lt;?php // get taxonomies terms links function custom_taxonomies_terms_links() { global $post, $post_id; // get post by post id $post = &amp;get_post($post-&gt;ID); // get post type by post $post_type = $post-&gt;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-&gt;ID, $taxonomy ); if ( !empty( $terms ) ) { $out = array(); foreach ( $terms as $term ) $out[] = '&lt;a href="' .get_term_link($term-&gt;slug, $taxonomy) .'"&gt;'.$term-&gt;name.'&lt;/a&gt;'; $return = join( ', ', $out ); } } return $return; } ?&gt; </code></pre> <p>Then I dropped in the echo call into the class call in my loop on the archive-portfolio.php page like this:</p> <pre><code> &lt;?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?&gt; &lt;div id="post-&lt;?php the_ID(); ?&gt;" class="&lt;?php echo custom_taxonomies_terms_links(); ?&gt;"&gt; </code></pre> <p>Any help would be greatly appreciated. This is driving me nuts that I can't figure this out.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload