WordPress.org

Ready to get started?Download WordPress

Forums

Conditional heading tags (1 post)

  1. Bodhipaksa
    Member
    Posted 7 years ago #

    I wanted to have different h3 classes on different categories but couldn't find anything on the forum about it. So I played around and concocted my first ever conditional statement. It worked! Yay me!

    I thought I'd share it in case there's anyone else out there who wants to do the same sort of thing. Incidentally, I'm sure there are other ways of doing this (I can think of at least one) but this does work.

    The h3 tags in my archive.php now read:

    <h3 class="entrytitle<?php /* Calls different class for each category */
    $post = $wp_query->post;
    if ( in_category('11') ) {echo "11";}
    elseif ( in_category('12') ) {echo "12";}
    elseif ( in_category('13') ) {echo "13";}
    elseif ( in_category('15') ) {echo "15";}
    ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>

    So this gives me various classes like <h3 class="entrytitle11">, <h3 class="entrytitle12"> etc.

    I can then style these however I want, for example with a different background image as an icon on each heading:

    h3.entrytitle11
    {
    padding:4px 5px 4px 28px;
    margin:0 0 5px 0;
    color:#333;
    font-weight:bold;
    background:#CBD6E2 url(img/h3icon-book.gif) no-repeat 3px 50%;
    }
    
    h3.entrytitle12
    {
    padding:4px 5px 4px 28px;
    margin:0 0 5px 0;
    color:#333;
    font-weight:bold;
    background:#CBD6E2 url(img/h3icon-quote.gif) no-repeat 3px 50%;
    }

    You can see the result on my test blog at http://test.wildmind.org/blogs/

Topic Closed

This topic has been closed to new replies.

About this Topic