WordPress.org

Support

Support » Themes and Templates » [Resolved] Change class according to post category

[Resolved] Change class according to post category

  • Hello all.

    I’ll try to be short and to the point.

    I have two categories on my blog. I want to use different formatting (both on index and single post page) for each of them. In order to do this, I’m trying to get the category name, then use an if statement to change the <div>’s class.

    Here’s the code I’m using at the moment but it doesn’t work properly.

    <?php
    $cat = get_category('name');
    if ($cat="test"){
    	$class="post-content-short";}
    else {
    	$class="post-content-long";}
    ?>
    
    <div class="<?php echo $class; ?>">
    	<?php the_content(); ?>
    </div>

    No matter what, the output is always “post-content-short”.

    This is probably an easy thing to do, but I’m a complete newb at PHP and I have (almost) no idea what I’m doing.

    Hope someone can help.

    Thanks!

Viewing 5 replies - 1 through 5 (of 5 total)
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[Resolved] Change class according to post category’ is closed to new replies.