• Hi,

    I want to be able to loop through the the categories that are attached to a single post so that the can be displayed individually.

    here is my site http://www.attackfromplanetb.com/, as you can see each post has a tab at the side displaying what category it is in. I want to show multiple category tabs.

    Any help would be great.

    Cheers

    Adam

Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter adz1809

    (@adz1809)

    to continue…

    this the html that I want to turn into a loop:

    <div class="post-cat-tabs">
    
    		<em class="cat-tab horror-tab" title="Horror"><span>Horror</span></em>
    
    		<em class="cat-tab scifi-tab" title="scifi"><span>scifi</span></em>
    
    		</div>

    so for each cat-tab class there is a loop

    Cheers again

    Thread Starter adz1809

    (@adz1809)

    Any help?

    could you post the php from index.php(?) that makes the html that displays the tab?

    a possible way to get all category names from the post could be:
    <?php $cats = wp_get_post_categories($post->ID); foreach($cats as $cat) { echo get_cat_name( $cat ).'<br />'; } ; ?>
    this snippet above will simply echo a list of the category names (no links, no formatting,..)

    the next, probably more complicated step, is to turn this into absolute positioned divs, which increase position with every category.

    to even begin to develop this, the original php code is essential.

    Thread Starter adz1809

    (@adz1809)

    here is the part of the index.php that display the category at the moment:

    <em class="cat-tab <?php the_category_unlinked(''); ?>-tab" title="
    <?php the_category_unlinked(''); ?>"><span><?php the_category_unlinked(''); ?></span></em>
    Thread Starter adz1809

    (@adz1809)

    The css part is sorted. It’s just writing out the loop that’s the issue

    Cheers for the help

    Thread Starter adz1809

    (@adz1809)

    is it something like this I should be creating:

    <?php $cats = wp_get_post_categories($post->ID); foreach($cats as $cat) { ?>
    
    <em class="cat-tab <?php echo get_cat_name( $cat ). ?>-tab" title="<?php echo get_cat_name( $cat ). ?>"><span><?php echo get_cat_name( $cat ). ?></span></em>
    
    <?php } ?>

    An alternative, the suggestion above should also be fine, and i don’t wish to cut into someone elses help…

    // Get categories for post
    $categories = get_the_terms( $post->ID , 'category' );
    // Create empty string
    $cat_string = '';
    // If categories
    if( $categories ) {
    	// Foreach of the array items returned
    	foreach( $categories as $key => $cat_obj ) {
    		// Append the string with category stuff for each result
    		$cat_string .= '<em class="cat-tab ' . $cat_obj->slug . '-tab" title="' . $cat_obj->name . '"><span>' . $cat_obj->name . '</span></em>';
    	}
    }
    // Echo the string, it's just an empty string if no data was found (so no errors)
    echo $cat_string;
    Thread Starter adz1809

    (@adz1809)

    Thank you all so much for your help everyone. It’s working like a dream.

    Cheers again.

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

The topic ‘Post category loop’ is closed to new replies.