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.

I am trying to set up my blog so that there are different colors for 2 different categories, however, if there is a post that is tagged with neither of these 2 categories I want it to default to green.

Here is my code:

    <?php if ( in_category( 'News' )) : ?>
    <div class="green">
    <?php elseif ( in_category('Blog')) : ?>
    <div class="orange">
    <?php endif; ?>  

So basically I want to change it so that if the post isn't in the "News" or the "Blog" category, it will just default to the "green" class.

Thanks in advance!

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

You should familiarize yourself with PHP, but if you want a default color ( assuming the code below already works ) you could do:

<?php if ( in_category( 'News' )) : ?>
    <div class="green">
<?php elseif ( in_category('Blog')) : ?>
    <div class="orange">
<?php else : ?>
    <div class="pink">
<?php endif; ?>
share|improve this answer
add comment

I'd suggest you use post_class for this. The following goes into your functions.php:

function category_name_post_class( $classes ) {
    global $post;
        $categories = get_the_category( $post->ID );
    foreach( $categories as $category)
        $classes[] = $category->category_nicename;
        return $classes;
}
add_filter('post_class', 'category_name_post_class');

Your div now looks like this:

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

which output some thing like:

<div class="your-category-name">

the styling is done via CSS as always.

share|improve this answer
add comment

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.