admin管理员组文章数量:1130349
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?
Share Improve this question asked Dec 13, 2018 at 16:50 lushiris02lushiris02 357 bronze badges 1 |2 Answers
Reset to default 0Here 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.
- All Single Product Pages - body.single-product .header-inner {}
- Specific Single Post - body.postid-### .header-inner {}
- Blog Page - body.blog .header-inner {}
- All Single Blog Pages - body.single-post .header-inner {}
- Etc...
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/
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?
Share Improve this question asked Dec 13, 2018 at 16:50 lushiris02lushiris02 357 bronze badges 1-
It's possible to do this without your conditional using only CSS, if you used the
body_classfunction 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
2 Answers
Reset to default 0Here 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.
- All Single Product Pages - body.single-product .header-inner {}
- Specific Single Post - body.postid-### .header-inner {}
- Blog Page - body.blog .header-inner {}
- All Single Blog Pages - body.single-post .header-inner {}
- Etc...
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/
本文标签: hooksIssues with ifelseand elseif statements
版权声明:本文标题:hooks - Issues with if, else, and elseif statements 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749101160a2316012.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


body_classfunction 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