Forums

[resolved] Multiple Headers using Conditional Tags (6 posts)

  1. andymelton
    Member
    Posted 3 years ago #

    I've been working to try and make a different header image to appear when a user clicks over to a specific category on the blog. I got it to work with pages. (I have that turned off at the moment) I am using the conditional tags that are defined in the Codex. I also want a random image to appear on pages or sections that don't have a specific image applied to them. Below is the code I am using. Anyone have any suggestions?

    <?php
    if (is_category(fedora-core-linux))
    {
    include(get_template_directory() . '/headers/fedora-header.php');
    }
    else if (is_category(free-software))
    {
    include(get_template_directory() . '/headers/free-header.php');
    }
    else if (is_category(microsoft-windows))
    {
    include(get_template_directory() . '/headers/windows-header.php');
    }
    else if (is_category(miscellaneous-tech))
    {
    include(get_template_directory() . '/headers/misc-header.php');
    }
    else if (is_category(ubuntu-linux))
    {
    include(get_template_directory() . '/headers/ubuntu-header.php');
    }
    else if (is_category(web-design-treasures))
    {
    include(get_template_directory() . '/headers/treasures-header.php');
    }
    else {
    include(get_template_directory() . '/images/headers/rotatable/rotate.php');
    }
    ?>

    If it helps, you can view the install I am working on at:

    http://blog.andymelton.net

  2. Otto42
    Moderator
    Posted 3 years ago #

    You should wrap the name of the category in quotes. Like so:
    is_category('fedora-core-linux')

    A lot of this switching functionality is already built in, so you might be better served by creating category templates instead of customizing the header. You could have templates for individual categories for specific images, and then a generic category template to do a random image.

    See here for more info:
    http://codex.wordpress.org/Category_Templates

    Similarly, you could use a generic page template for pages, and indivdual page templates for specific pages:
    http://codex.wordpress.org/Template_Hierarchy#PAGE_page

    Done in the most obvious way, this does duplicate a lot of code, however you could pull the duped parts out into a more generic file and then simply include them from the templates, to prevent repetition.

  3. andymelton
    Member
    Posted 3 years ago #

    Otto, thank you SO much for your reply. I added the quotes, that did not work. However, I am going to look into the category templates idea that you suggested. That sounds like a much better way of doing what I am trying to do anyways...hopefully, less annoying! Again. Thanks, SO much for your reply, the code needed a fresh pair of eyes! Thanks!

  4. moshu
    Member
    Posted 3 years ago #

    Here is an old thread with a lot of step by step instructions
    http://wordpress.org/support/topic/30653?replies=16
    (the code is in another topic linked from there!)

  5. andymelton
    Member
    Posted 3 years ago #

    Oh and also, it will take care of the issue I was facing of when the user is in a category and they want to view a single post it was going to display a random image, so, maybe, I am hoping, this will take care of that as well. Thanks again!!!

  6. andymelton
    Member
    Posted 3 years ago #

    Ok, so I know I have replied a LOT...but, I just wanted to say that it DID work...I just forgot to say "OK" to the overwrite of the current file!!! :) Whoops!

Topic Closed

This topic has been closed to new replies.

About this Topic