0

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!

3 Answers 3

0

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'; }  ?> ">  
0
0

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; ?>
0

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;
}
2
  • 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! Oct 5, 2011 at 22:28
  • @TheFragileMachine sorry didn't have time to update my answer, this code will do everything as you want
    – Mamaduka
    Oct 10, 2011 at 11:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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