Support » Themes and Templates » Styling Different Headers Using Different Classes – via PHP

  • Resolved EMG


    My Topic Title stinks, but I couldn’t think of any other way to describe what I’m trying to do – whooops.

    So I’m using something like this:

    <div id="header" <?php if ( has_tag('books')) {
    echo 'class="books"'; } ?>>

    to add classes to my #header to help me style a different header for certain different SINGLE posts, post groupings (specific categories or pages), etc.

    What I Am Trying To Do (Recap): I would like to be able to specify different header specifications via CSS for individual posts (via tags probably), different pages, etc. I would also like for my home/main/most recent listing of posts to have a static header that will never change regardless of the tags the recent posts may have.

    The example I gave works just splendidly and if the single post in question is displaying the ‘books’ tag, the header will be styled for the books class, but I would like to do more.

    I would like to be able to specify classes for not just the ‘books’ tag but also for a ‘movies’ tag and whatever other non-overlapping tag (meaning, a post tagged with ‘books’ will never be tagged with ‘movies’) I might have that I might have a custom header image for.

    ALSO, I would like my recent posts page’s header to remain as is and to not change based on my list of collective recent posts and their tags.

    Is there any way to do this using the code that I have started?

    I know that there are rotating header images plugins available, but my header stylization – image included – are all in my CSS stylesheet (background image) and I would like to keep it this way. The plugins I have seen have all used <img src... /> to embed the header image (or media).

    I also know that I could create a bunch of custom templates and in each template, call a different header template with a different stylesheet, but… that just seems even more complicated and unnecessary than what I am already doing.

    Or is it?

    I’m no PHP expert of any sort and I need some help and would be really grateful if someone could offer it.

    Please and thank you!

    EDIT: I have discovered that:

    <div id="header" <?php if (is_single(has_tag('books'))) {
    echo 'class="books"'; } ?>>

    will style ONLY posts that are tagged with ‘books’ and NOT my main/home page, so at least -that- works to ensure my home page remains styled as is.

    But I would still like to style more than one tag in one function if possible.

Viewing 4 replies - 1 through 4 (of 4 total)
  • EMG


    I can’t edit the original post anymore otherwise I would, but I changed the location of the PHP function to be in the <body> tag instead so I can style other parts if needed and not just the header. Not sure if this information was/is pertinent so I decided to add it anyways just in case.

    Still lost on the inclusion of the other things I am trying to do, though.



    Forum Moderator

    have you tried body_class() ?

    otherwise you could try and use if elseif statements; for instance:

    <div id="header" <?php if (is_single()) :
    if(has_tag('books'))) {
    echo 'class="books"'; }
    elseif(has_tag('movies'))) {
    echo 'class="movies"'; }
    endif; ?>>

    php if/elseif



    Hi there! 🙂

    Thanks for the response; I will definitely look into using thebody_class() and thank you for the example and the links!

    I will report back once I implement things.



    Reporting back to say that after some wrangling, I decided to utilize a function that would call up a different stylesheet per tag/specification.

    The method I mentioned previously which alchymyth expounded on (adding classes to the body tag) does work, but it’s long and considering my list of tags, it’s also unwieldy. In an effort to clean up my code, I went the way of a function instead which shoves all the PHP stuff into the functions.php file.

    The body_class() works also in auto-classing the different posts and pages, but with limitations if the body_class() tag is used as-is and without modifications.

    If I were a better at piecing together PHP I might have tackled the body_class() method of adding additional styling classes, but I’m not and utilizing a function allows me the freedom to separate each tag’s/specification’s into its own separate child and grandchild stylesheet which would let me re-style the entire theme and not just the header if I so desired and independent of additional classes which eliminates potential cascading styling issues.

    Thanks to alchymyth for all the pointers; it really helped to set me in the right direction! 🙂

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Styling Different Headers Using Different Classes – via PHP’ is closed to new replies.