$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>hooks - Issues with if, else, and elseif statements|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

hooks - Issues with if, else, and elseif statements

matteradmin9PV0评论

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_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
Add a comment  | 

2 Answers 2

Reset to default 0

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.

  • 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/

Post a comment

comment list (0)

  1. No comments so far