WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Problem styling based on category (7 posts)

  1. MagnusCreed
    Member
    Posted 1 year ago #

    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 {
    }
    ?>
  2. esmi
    Forum Moderator
    Posted 1 year ago #

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

  3. MagnusCreed
    Member
    Posted 1 year ago #

    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?

  4. esmi
    Forum Moderator
    Posted 1 year ago #

  5. MagnusCreed
    Member
    Posted 1 year ago #

    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

  6. esmi
    Forum Moderator
    Posted 1 year ago #

    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.

  7. MagnusCreed
    Member
    Posted 1 year ago #

    Thank you, your tip was enlightening.

Topic Closed

This topic has been closed to new replies.

About this Topic