Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to change header and menu based on what category a post is listed in.

I could duplicate the post and assigned each of the two categories separately but this sounds messy and bleh.

The post/s in this example belong in both "pirtek" and "btcc".

http://www.talkativebroadcasting.co.uk/

but the posts with pirtek need to be styled with custom header etc. (and eventually custom menu...which makes me think perhaps I should just duplicate for easy implementation of a seperate menu?)

What do you think? As it stands my standard header is prioritised every time

    <php
if ( has_category('pirtek') ) {
  $header = 'http://www.talkativebroadcasting.co.uk/wp-content/uploads/2013/11/cropped-pirtekheader.png'
} else {
  $header = 'http://talkativebroadcasting.co.uk/wp-content/uploads/2013/06/cropped-talkativeheader2.png'
}
?>
share|improve this question
    
Post your code, please, but it should be easy enough to give pirtek priority. –  s_ha_dum Nov 4 '13 at 0:58
    
Maybe your problem is probably the use of relative urls. Rigth way to retrieve the full url of your uploads folder is to use wp_upload_dir function. After that, why are you calling get_header_image() if then you use different images urls? –  G. M. Nov 4 '13 at 12:02
    
How about my updated code? That is still not showing any header. Am I getting the wrong syntax? –  Sam James Nov 4 '13 at 14:00
    
hmmm will category templates be a better option? Will that give me the option of displaying a different menu entirely if that category is selected? –  Sam James Nov 4 '13 at 15:39

2 Answers 2

up vote 1 down vote accepted

One easy way is to use has_category function.

E.g. in your header you can use

if ( has_category('pirtek') ) {
  $header = '/path/to/pirtek/header'
} else {
  $header = '/path/to/standard/header'
}

If the post has the 'kirtek' category, then the condition inside if is true, no matter what other categories the post belongs to.

This kind of if statement can be used everywhere you need, however, the snippet above works well in singular templates and inside the loop. If you want to use that conditional outside the loop, you need to pass the post object as second argument of has_category.

$postid = 10;

if ( has_category( get_post($postid) ) ) {
  // do something 
}
share|improve this answer
    
thanks very much. having a bit of beginner woes but i understand what it should be doing. my guess is its slightly wrong as I am not getting a header at the moment! Please see revised initial question. Thanks –  Sam James Nov 4 '13 at 11:53
<?php 
if ( has_category('pirtek') ) {
$header_image = "http://www.talkativebroadcasting.co.uk/wp-content/uploads/2013/11/cropped-pirtekheader.png";
        if ( ! empty( $header_image ) ) : ?>
            <a href="<?php echo esc_url( "http://www.pirtek.co.uk/" ); ?>"><img src="<?php echo esc_url( $header_image ); ?>" class="header-image" width="<?php echo get_custom_header()->width; ?>" height="<?php echo get_custom_header()->height; ?>" alt="" /></a>
        <?php endif; }

else {
$header_image = "http://talkativebroadcasting.co.uk/wp-content/uploads/2013/06/cropped-talkativeheader2.png";
        if ( ! empty( $header_image ) ) : ?>
            <a href="<?php echo esc_url( home_url( '/' ) ); ?>"><img src="<?php echo esc_url( $header_image ); ?>" class="header-image" width="<?php echo get_custom_header()->width; ?>" height="<?php echo get_custom_header()->height; ?>" alt="" /></a>
        <?php endif; }

?>

For reference this code in header.php does exactly what I need. Many thanks!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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