简体   繁体   English

PHP if语句“ if X和/或Y”

[英]PHP if statement “if X and/or Y”

I am trying to create a less clunky version of the following if conditional in a Wordpress theme. 如果有条件以Wordpress为主题,我尝试创建以下内容的简洁版本。 I am trying to figure out how to do it without opening and closing the style tag twice: 我试图弄清楚如何做而无需两次打开和关闭样式标签:

<?php if (get_theme_mod( 'main_color' )) : ?>
  <style>
    #branding {
        background: <?php echo get_theme_mod( 'main_color', '#243964' )."\n"; ?>;
    }
    a:link {
        color: <?php echo get_theme_mod( 'main_color', '#243964' )."\n"; ?>;
    }
  </style>
  <?php endif ?>
  <?php if (get_theme_mod( 'links_color' )) : ?>
  <style>
    #wrapper a:link {
        color: <?php echo get_theme_mod( 'links_color', '#6C84B4' )."\n"; ?>;
    }   
  </style>
  <?php endif ?>

I tried to combine the two with the following: 我尝试将以下两者结合起来:

<?php if (get_theme_mod( 'main_color' || 'links_color' )) : ?>
      <style>
        #branding {
            background: <?php echo get_theme_mod( 'main_color', '#243964' )."\n"; ?>;
        }
        a:link {
            color: <?php echo get_theme_mod( 'main_color', '#243964' )."\n"; ?>;
        }
        #wrapper a:link {
            color: <?php echo get_theme_mod( 'links_color', '#6C84B4' )."\n"; ?>;
        }   
      </style>
      <?php endif ?>

But for some reason this doesn't work. 但是由于某种原因,这是行不通的。

Any help? 有什么帮助吗?

尝试:

<?php if (get_theme_mod( 'main_color') || get_theme_mod( 'links_color' )) : ?>
<?php if (get_theme_mod( 'main_color' || 'links_color' )) : ?>

should be 应该

<?php if (get_theme_mod( 'main_color') || get_theme_mod('links_color') )) : ?>

Because get_theme_mod() is a function you pass parameters to it. 因为get_theme_mod()是一个函数,所以您要将参数传递给它。 You could change the function to accept several paramiters then you could call the function like : 您可以将函数更改为接受几个参数,然后可以像下面这样调用该函数:

<?php 
function get_theme_mod($main_color, $links_color) {
   // do stuff and return bool
}

if (get_theme_mod( 'main_color', 'links_color' )) : 
?>

Also it's not the main problem with what you're trying to do but you should terminate <?php endif; ?> 同样,这不是您要执行的操作的主要问题,但是您应该终止<?php endif; ?> <?php endif; ?>

After reading your post again, I guess this is what you try to achieve: 再次阅读您的文章后,我想这是您尝试实现的目标:

<style>
<?php if (get_theme_mod( 'main_color' )) : ?>
    #branding {
        background: <?php echo get_theme_mod( 'main_color', '#243964' )."\n"; ?>;
    }
    a:link {
        color: <?php echo get_theme_mod( 'main_color', '#243964' )."\n"; ?>;
    }
<?php endif ?>
<?php if (get_theme_mod( 'links_color' )) : ?>
    #wrapper a:link {
        color: <?php echo get_theme_mod( 'links_color', '#6C84B4' )."\n"; ?>;
    }   
<?php endif ?>

</style>

This will only show the wanted css code, depending on the theme options, but you will only have one <style> tag opening and closing. 这将仅显示所需的CSS代码,具体取决于主题选项,但您只能打开和关闭一个<style>标签。

<?php if (get_theme_mod( 'main_color' ) || get_theme_mod( 'links_color' )) : ?>
<style type="text/css">
<?php endif ?>
<?php if (get_theme_mod( 'main_color' )) : ?>
#branding {
    background: <?php echo get_theme_mod( 'main_color', '#243964' )."\n"; ?>;
}
a:link {
    color: <?php echo get_theme_mod( 'main_color', '#243964' )."\n"; ?>;
}
<?php endif ?>
<?php if (get_theme_mod( 'links_color' )) : ?>
#wrapper a:link {
    color: <?php echo get_theme_mod( 'links_color', '#6C84B4' )."\n"; ?>;
}   
<?php endif ?>

<?php if (get_theme_mod( 'main_color' ) || get_theme_mod( 'links_color' )) : ?>
</style>
<?php endif ?>

The above answers are good but I think they're missing something. 上面的答案很好,但我认为它们缺少一些东西。

The original code will: 原始代码将:

  • add certain styles if condition A is met. 如果满足条件A,则添加某些样式。
  • It will add additional styles if condition B is met. 如果满足条件B,它将添加其他样式。

Combining them like this: 像这样组合它们:

<?php if (get_theme_mod( 'main_color') || get_theme_mod( 'links_color' )) : ?>

Fixes the error in the OP's attempt but the OP's attempt was not an exact solution to their original goal of simply printing "less style tags" . 修复了OP尝试中的错误,但是OP的尝试并不能完全解决其仅打印"less style tags"的原始目标。 This is because if either one of those conditions are met, BOTH styles will be applied. 这是因为如果满足这些条件之一,则将同时使用两种样式。

(perhaps this is the desired functionality?) (也许这是所需的功能?)

Geralds answer is closer because it sticks to the original logic but it will always print a STYLE tag, regardless if it's met. Geralds的答案更为接近,因为它遵循原始逻辑,但是无论是否满足要求,它始终会打印一个STYLE标签。 Given the original posters desire to "clean up their HTML", there can still be a better solution of posting a STYLE tag only if there's styles to print. 考虑到原始海报希望“清理HTML”,只有在有样式需要打印的情况下,仍然存在更好的发布STYLE标签的解决方案。

The solution would be to perform all the logic in PHP, add the HTML to a buffer and then print that buffer if it's not empty. 解决方案是执行PHP中的所有逻辑,将HTML添加到缓冲区中,如果缓冲区不为空,则打印该缓冲区。 You could do this by using the actual buffer ob_start() or just saving the string into a variable. 您可以通过使用实际的缓冲区ob_start()或将字符串保存到变量中来实现。 I'll use the more straight forward "save to a variable" approach. 我将使用更直接的“保存到变量”方法。

<?php
// initialize the buffer we'll save our style strings too
$buffer = '';
// save the styles to the buffer for main
if (get_theme_mod( 'main_color' )) {
    $buffer .= 
    "#branding {
         background: " . get_theme_mod( 'main_color', '#243964' ) . "\n
    }
    a:link {
         color: " . get_theme_mod( 'main_color', '#243964' ) . "\n
    }\n";
}
// save the styles to the buffer for link colors
if (get_theme_mod( 'links_color' )) {
    $buffer .= 
    "#wrapper a:link {
          color: " . get_theme_mod( 'links_color', '#6C84B4' ) . "\n
    }\n";
}

// verify there's styles to print and print them into a single STYLE tag
if(!$buffer) {
    echo "<style>" . $buffer . "</style>";
}

?> ?>

This will: 这将:

  1. Keep the same logic in terms of when additional styles are added 在添加其他样式时保持相同的逻辑
  2. Only Print one STYLE tag 仅打印一个STYLE标签
  3. Only print a STYLE tag if there are styles to print 仅在有样式要打印时才打印STYLE标签

I hope that helps! 希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM