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.

What im trying to do is echo a class depending on witch of 3 Main "Parent" Categories the content was posted in , here is my code so far

<article class="post 
<?php 
if ( in_category( 'dream-it' )){ echo 'dreamit'; } 
if ( in_category( 'build-it' )){ echo 'buildit'; }
if ( in_category( 'get-the-hell-out' )){ echo 'getout'; } 
?>
"> 

I know im doing this wrong any help would be amazing ! thank you!

share|improve this question

3 Answers 3

up vote 0 down vote accepted

you probably need to get the parent category of each of the post's categories first.

<?php
$parent_cat = array();
$post_cats = get_the_category($post->ID);
  foreach( $post_cats as $post_cat ) {
  if( $post_cat->parent ) $parent_cat[] = get_category( $post_cat->parent )->slug;
  }
?>
<article class="post  <?php  
  if ( in_array( 'dream-it', $parent_cat ) ) { echo ' dreamit'; }  
  if ( in_array( 'build-it', $parent_cat ) ) { echo ' buildit'; } 
  if ( in_array( 'get-the-hell-out', $parent_cat ) ) { echo ' getout'; }  ?> ">  
share|improve this answer
    
This worked thank you!! –  The Fragile Machine Oct 6 '11 at 15:10

I used this code for something similar on a project in the past, basically I needed to display content depend of the category, hope it works for you! Peace!

  <?php if (in_category('CATEGORY_1') ) : ?>
    WHATEVER YOU WANT TO DISPLAY
  <?php elseif (in_category('CATEGORY_2') ) : ?> 
    WHATEVER YOU WANT TO ECHO
  <?php elseif (in_category('CATEGORY_3') )  : ?>
    WHATEVER YOU WANT TO ECHO
  <?php else : ?>       

  WHATEVER ELSE :)

  <?php endif; ?>
share|improve this answer

Use post_class() function for that, like so:

<article <?php post_class(); ?>>

for more information check functions codex page - http://codex.wordpress.org/Function_Reference/post_class

Update

Put following code in your functions.php

add_filter( 'post_class', 'mamaduka_add_parent_category_class' );
/**
 * Add parent category class to post_class() function
 *
 */
function mamaduka_add_parent_category_class( $classes ) {
    global $post;

    foreach ( (array) get_the_category( $post->ID ) as $cat ) {
        if ( ! empty( $cat->parent ) ) {
            $parent_cat = &get_category( $cat->parent );
            $classes[] = 'category-parent-' . sanitize_html_class( $parent_cat->slug, $parent_cat->term_id );
        } else {
            $classes[] = 'category-parent-' . sanitize_html_class( $cat->slug, $cat->term_id );
        }
    }
    return $classes;
}
share|improve this answer
    
Cool thanks for the response! I might not be quite understanding this method so far, but it does not seem to work for me. All my posts are in sub-categories of the main parent, and im trying get the the div to change a class depending on witch parent that post / sub-category was posted under. I hope that makes a little more sense, again thanks a ton for the help! –  The Fragile Machine Oct 5 '11 at 22:28
    
@TheFragileMachine sorry didn't have time to update my answer, this code will do everything as you want –  Mamaduka Oct 10 '11 at 11:18

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.