WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Problem styling based on category

[Resolved] Problem styling based on category

  • MagnusCreed

    @magnuscreed

    I am trying to style an h2 based upon what category the post is in.

    I have made this code and placed it within the loop and all works well…for the first of each ‘cat_name==’. As soon as a 2nd/3rd/etc post is categorized as ‘Branding’, those subsequent posts dont get styled at all.

    <?php
         foreach((get_the_category()) as $category) {
         if ($category->cat_name == 'Branding') {
         echo the_date('m/d', '<h2 class="postDateBrnd postBar">', '</h2>'); }
         elseif ($category->cat_name == 'Consulting') {
         echo the_date('m/d', '<h2 class="postDateCnslt postBar">', '</h2>'); }
         else {
    }
    ?>
Viewing 6 replies - 1 through 6 (of 6 total)
  • esmi

    @esmi

    Forum Moderator

    Does your theme use the body_class() function on its opening <body> element?

    MagnusCreed

    @magnuscreed

    It does not. I am just learing to make wordpress themes and this would be the first I have heard of this function.

    Should it be in there?

    esmi

    @esmi

    Forum Moderator

    MagnusCreed

    @magnuscreed

    Ok, just want to make sure I am understanding. The body_class() function is a better way of doing what i tried to do above and isnt a tag on to what is above?

    Also, i want to make sure that I am clear on what I am doing. I am trying to display the headings for each category differently but within the same page. ie. in my blog roll, i am listing the category and trying to style each category heading with a different background image.

    I am just clarifying as what i am reading seems to be more related to styling differently for a category when that category might be on its own page or be a single page view. One of the examples is:

    To add a category class to single post pageviews and template files

    esmi

    @esmi

    Forum Moderator

    The body_class() function is a better way of doing what i tried to do above

    Correct. You may also want to ensure that your theme uses the post_class() function.

    Thank you, your tip was enlightening.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘[Resolved] Problem styling based on category’ is closed to new replies.
Skip to toolbar