For my site, I need to have different header colors depending on the page it's on. Here's the code I have for my site that is working:
<?php if ( is_single() && is_post_type('product') || is_page(576) || is_single() && is_post_type('post')) : ?>
<div class="header-inner">
<style type="text/css">.header-inner { background-color:#861919!important; }</style>
<?php else : ?>
<div class="header-inner" style="background-color:rgba(0,0,0,0.30);">
<?php endif; ?>
I am trying to modify the above to exclude certain product posts due to their design. I will note that most products use post-template.php, and the ones I need to have a transparent header are using product-specialty.php. My developer installed the plugin WP Post to be able to select the different templates. Here's my attempt at the modified code but it's resulting in nothing loading:
<?php if ( is_single(893,892,843,895,894,896) ) : ?>
<div class="header-inner" style="background-color:rgba(0,0,0,0.30);">
<?php else( is_single() && is_post_type('product') || is_page(576) || is_single() && is_post_type('post')) : ?>
<div class="header-inner">
<style type="text/css">.header-inner { background-color:#861919!important; }</style>
<?php elseif : ?>
<div class="header-inner" style="background-color:rgba(0,0,0,0.30);">
<?php endif; ?>
Any idea what I'm doing wrong there?
For my site, I need to have different header colors depending on the page it's on. Here's the code I have for my site that is working:
<?php if ( is_single() && is_post_type('product') || is_page(576) || is_single() && is_post_type('post')) : ?>
<div class="header-inner">
<style type="text/css">.header-inner { background-color:#861919!important; }</style>
<?php else : ?>
<div class="header-inner" style="background-color:rgba(0,0,0,0.30);">
<?php endif; ?>
I am trying to modify the above to exclude certain product posts due to their design. I will note that most products use post-template.php, and the ones I need to have a transparent header are using product-specialty.php. My developer installed the plugin WP Post to be able to select the different templates. Here's my attempt at the modified code but it's resulting in nothing loading:
<?php if ( is_single(893,892,843,895,894,896) ) : ?>
<div class="header-inner" style="background-color:rgba(0,0,0,0.30);">
<?php else( is_single() && is_post_type('product') || is_page(576) || is_single() && is_post_type('post')) : ?>
<div class="header-inner">
<style type="text/css">.header-inner { background-color:#861919!important; }</style>
<?php elseif : ?>
<div class="header-inner" style="background-color:rgba(0,0,0,0.30);">
<?php endif; ?>
Any idea what I'm doing wrong there?
Here is an alternative solution. I think it would be cleaner than adding all these if statements and page IDs. You can do this using CSS and the body class WP adds.
For example, go to one of your single pages and look at the class on the body tag. You should see something like page-id-###. Then just simply add a rule in your stylesheet or the CSS customizer.
Something like this...
body.page-id-123 .header-inner,
body.page-id-124 .header-inner,
body.page-id-125 .header-inner, {
background-color:#861919!important;
}
Here is how you can target some of your other pages.
Just inspect each page and you should get the idea.
Create your own body class
If needed you could create your own body class by using the body_class filter, here is a basic example of how to use it. Of course you could do more by adding conditionals or checking for templates.
function my_custom_body_class($classes) {
$classes[] = 'foo';
return $classes;
}
add_filter('body_class', 'my_custom_body_class');
You are just passing through multiple values instead of a list. Switch to an array.
Change this line:
<?php if ( is_single(893,892,843,895,894,896) ) : ?>
to:
<?php if ( is_single(array(893,892,843,895,894,896)) ) : ?>
More info: https://developer.wordpress/reference/functions/is_single/
body_class
function correctly, the body tag will have a css class with the page ID you can select against, and you get to avoid the inline style tags too – Tom J Nowell ♦ Commented Dec 13, 2018 at 17:19