0

I am using a simple if/elseif/else bit of code to generate a header image for my page, I can't seem to understand why it isn't working properly. It determines the category of the page being displayed and displays a header accordingly. Here's the code in my header.php:

<?php if (is_category('dino run') || has_category('dino run')) { ?>
    <a href="dino-run/"><li><div class="logo splash2">Dino Run</div></li></a>           
<?php } elseif (is_category('potatoman') || has_category('potatoman')) { ?>
    <a href="http://www.pixeljam.com/potatoman/"><div class="logo splash4">Potatoman Seeks the Troof</div></a>
<?php } elseif (is_category('glorkian' || 'glork') || has_category('glorkian' || 'glork')) { ?>
    <a href="glorkian"><div class="logo splash3">Glorkian Warrior</div></a>
<?php } else { ?>
        <a href="#"><div class="logo splash1">The Pixeljam Octology</div></a>
<?php } ?>

The problem I'm having is I click on a post that has a category "Dino Run" and it displays fine... but when I click on a post that doesn't have any of the above 3 defined categories it doesn't fall through to the else statement, it actually displays the third header "Glorkian Warrior".

Any ideas what I did wrong here?

  • In the future, please include checking the Codex function reference as part of normal debugging, before posting questions. Questions that are answered with merely a link to the Codex are generally considered as low-quality. – Chip Bennett Jan 9 '14 at 15:19
3

is_category will not process the logic inside the argument list. This:

is_category('glorkian' || 'glork')

The condition is true on all category pages. What I think is happening is that PHP does parse that argument string, but is always going to be true. Try this:

var_dump('glorkian' || 'glork');

Meaning that what you are doing, essentially, is this:

is_category(true);

That is not a valid argument but the function seems to revert to default functionality-- is this a category archive or not? Obviously, the answer is "yes" on any category archive.

Valid arguments are:

(mixed) (optional) Category ID, Category Title, Category Slug or Array of IDs, names, and slugs.

http://codex.wordpress.org/Function_Reference/is_category#Parameters

So I believe that what you need is :

is_category(
  array(
    'glorkian',
    'glork'
  )
);

It is the same with has_category.

0

I would use:

in_category('cat-slug') ) { 

if you want to display the image on all posts in the category.

Here's a working example i wrote and tested.

This returns true on both the category archive page and all posts in the category

if(is_home())

echo '<div class="home-header"><img src="http://yourdomain.com/path/to/image.png" alt="category header image" /></div>';

elseif(in_category('category-1') )

echo '<div class="category-one-header"><img src="http://yourdomain.com/path/to/image.png" alt="category header image" /></div>';

elseif(in_category('category-2') )

echo '<div class="category-two-header"><img src="http://yourdomain.com/path/to/image.png" alt="category header image" /></div>';

elseif(in_category('category-3') )

echo '<div class="category-three-header"><img src="http://yourdomain.com/path/to/image.png" alt="category header image" /></div>';

elseif(in_category('category-4') )

echo '<div class="category-four-header"><img src="http://yourdomain.com/path/to/image.png" alt="category header image" /></div>';

}

Related info

Another option would be to use the code from the Twenty Eleven themes header.php file which enables you to use the built in Add Featured Image meta box to display unique headers on different posts and pages without adding any code.

Or you could easily add conditionals and else if statements to that code which is a more efficient solution.

  • Thanks, yeah using in_category works to remove the OR statement and having to use two methods to find out what the page is before deciding what to display. If only I could figure out a way to get things random. I was thinking about setting a bunch of arrays with two variables each and it would randomize which array it used and then pull out each array's value to make sure that the header and background image were matching. – ChaBuku Bakke Jan 10 '14 at 1:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.