WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Different class for each category (6 posts)

  1. marcoapaulo
    Member
    Posted 1 year ago #

    Hi everyone,

    I was looking for some kind of code that changed the class of a certain div or span depending on the category of the post.

    I've searched the forum (and on google) and I think I've found what I'm looking for, but, for some reason, that I'm not aware of, the "thing" isn't working.

    What I've tried to use were these two...

    http://pastebin.com/VSgsWpfZ and http://pastebin.com/bvdw5Krg

    It's almost the same thing, however the first one will return the default class for the posts instead of a special class for a specific category, which is better for me, since I don't have to create a new category to the default posts.

    What I want to do is this: I want to change the "mrc" (in the link below) to "category1class" or "category2class" depending on the category of the post.
    http://pastebin.com/3ZsmWeVt

    And what I do is this...
    http://pastebin.com/uvPFEmhn

    So, why is not working? Am I doing something wrong?

  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

  3. marcoapaulo
    Member
    Posted 1 year ago #

    Hmm it's giving me an error...

    This is the code http://pastebin.com/f3jbJguv

    The error is this "Parse error: syntax error, unexpected T_ENDWHILE in /.../wp-content/themes/themename/index.php on line 43" (the last line in the code shown in the link)

  4. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    you are missing a bracket } in the if statement, and an 'elseif';

    corrected section:

    <div class="<?php
        if ( in_category( 'category1' )) {
            echo "category1class";
        } elseif ( in_category( 'category2' )) {
            echo "category2class";
        } else {
            echo "generalcategoryclass";
        }
        ?>">
  5. Jose Castaneda
    Member
    Posted 1 year ago #

    Why not use post_class()

  6. marcoapaulo
    Member
    Posted 1 year ago #

    Thank you! Worked like a charm :)

Topic Closed

This topic has been closed to new replies.

About this Topic