• Hi,

    I’m running my portfolio with WordPress and created a custom post type called “portfolio”, with a couple taxonomies (categories for my portfolio items).

    I’d like to use the name of the taxonomies as CSS classes. I was using “<?php echo $term->slug; ?>”, but I changed my query, because I had some items showing twice, since they are listed under 2 taxonomies.

    Here is my new code :

    <ul id="portfolio">
                <?php query_posts('post_type=portfolio&posts_per_page=200'); if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
                 <li class="<?php echo $term->slug; ?>">
                 <a href="<?php echo get_permalink($term_post->ID); ?>" title="<?php echo get_the_title($term_post->ID) ?>"><?php echo get_the_post_thumbnail($term_post->ID, 'slider'); ?><span class="title"><?php echo get_the_title($term_post->ID) ?></span></a></li>
                 <?php endwhile; wp_reset_query(); ?>
                 </ul>

    Any help would be appreciated,
    Thanks in adavance !

Viewing 7 replies - 1 through 7 (of 7 total)
  • I’m not seeing where you’re getting the terms for the individual posts.., where is $term being pulled from? for that matter, $term_post…

    Thread Starter Hoppipolla

    (@hoppipolla)

    Hi,

    My taxonomies are created in funtions.php :

    add_action( 'init', 'build_taxonomies', 0 );
    
    function build_taxonomies() {
        register_taxonomy( 'cat_portfolio', 'portfolio', array( 'hierarchical' => true, 'label' => 'Catégories', 'query_var' => true, 'rewrite' => true ) );
    }

    Hope this helps ?

    Right, I understand that, but in that loop you have up there you’re not getting the term for the current post in that loop

    Thread Starter Hoppipolla

    (@hoppipolla)

    That’s my question, actually. I don’t know how to do this.

    <ul id="portfolio">
    	<?php
    	$args = array(
    			'post_type' => 'portfolio',
    			'posts_per_page' => -1 // all posts
    			);
    	$all_posts = get_posts($args);
    
    	foreach ($all_posts as $the_post) {
    		$terms = wp_get_post_terms( $the_post->ID, 'category' ); ?>
    		<li <?php if (!is_array($terms) && (count($terms) > 0)) { ?> class="<?php foreach ($terms as $term) { echo 'cat-'.$term->slug.' '; } ?>"<?php } ?>><a href="<?php echo get_permalink($the_post->ID); ?>" title="<?php echo get_the_title($the_post->ID) ?>"><?php echo get_the_post_thumbnail($the_post->ID, 'slider'); ?><span class="title"><?php echo get_the_title($the_post->ID) ?></span></a></li>
    	<?php } ?>
    	</ul>

    =)

    Sorry the !is_array($terms) is supposed to be just is_array($terms) without the ! (not)

    Thread Starter Hoppipolla

    (@hoppipolla)

    Hi,

    Thanks a lot for this, but it doesn’t seem to work ?

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘CUstom taxonomies & CSS class’ is closed to new replies.