0

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'
}
?>
4
  • Post your code, please, but it should be easy enough to give pirtek priority.
    – s_ha_dum
    Commented Nov 4, 2013 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?
    – gmazzap
    Commented Nov 4, 2013 at 12:02
  • How about my updated code? That is still not showing any header. Am I getting the wrong syntax?
    – Sam James
    Commented Nov 4, 2013 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
    Commented Nov 4, 2013 at 15:39

2 Answers 2

1

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 
}
1
  • 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
    Commented Nov 4, 2013 at 11:53
0
<?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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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