php - 我如何将背景颜色与 PHP 匹配以匹配文本颜色?

标签 php css wordpress advanced-custom-fields

我是 PHP 的初学者!我知道以前有过这样的问题,但是,它们并不像我想要的那样对我有用。

事情是这样的: 我有多个使用 css 和 php 实现的背景图像。到目前为止,这些图像中的每一个都是黑色、白色和蓝色。我希望能够编写一些 php...同样,我是初学者所以请原谅我的无知...将背景图像更改为上面列出的其中之一以匹配文本的颜色。

一些可能需要注意的事项: 这是在 php 文件上完成的。该文件还对应一个wordpress(ACF)插件。我可以更改此 wordpress 插件上的文本颜色。我试过使用 github,我看过这个网站和谷歌以前的答案,但是,我相信我对 php 和 javascript 的知识缺乏让我更加困惑。我认为我想要实现的目标可以通过简单的 if else 语句来完成。另外值得注意的是,我只想要 php 的答案,而不是 javascript 或 jquery 或 github(因为这些答案只会让我更加困惑)。请帮忙,谢谢。

这是我到目前为止编写的相关代码的片段。

#logo{
   position:absolute;
   top: 17px;
   left: 14px;
   height: 20px;
   padding: 7px 0 0 85px;

   background: url(<?php if $color_text = ("#FFF") 
                {echo ((<? php bloginfo("stylesheet_directory")?>)'/img/image_white.png');}?>);
   /*I wrote the above for background, I see now that you cannot
     have a php statement inside another php statement, How can I fix this?*/

   /*The below is what has been written before*/
   /*background: url(<?php bloginfo("stylesheet_directory")?>/img/image_white.png) no-repeat 0 2px;*/

  /*The below is how text color is determined in the wordpress plugin*/
  /*ignore colors_2 (it responds to a color picker) but I don't want to use it
     colors_text responds to the wordpress plugin where the user can choose white, black, or blue
   I would assume I would need to change the below php statement to work with the php statement regarding the background color change. (I would appreciate help doing this as well)*/

  color: <?php if ($colors_2 || $color_text) {echo ($color_text ? $color_text : $colors_2);}
   else echo '#005b92';?>;
   }

最佳答案

由于您的 if 语句有些复杂,因此不建议将其写成 ternary operator (condition ? true : false),最好使用 if 语句。

在您的示例中,您使用的是变量名称 $colors_text$colors_2,但是您没有使用 ACF 字段值填充这些变量。您可以使用 get_field() 执行此操作函数返回值,the_field()回显值(类似于循环中的 WordPress 函数:例如 the_title()get_the_title())。您可以在 if 语句中使用 get_field(),因为如果未设置该字段,它将返回 false

您还有一些语法错误。虽然您确实不能在 PHP 标记中嵌入 PHP 标记,但您的“背景”if 语句通常是不正确的。您有 if $color_text = ("#FFF") 但应该有 if ($color_text == "#FFF") - 左括号在错误的位置并且您正在使用 = (赋值)而不是 == (逻辑或)。我认为你试图只在颜色为白色时设置背景图像(或更准确地说是“#FFF”,因为“#FFFFFF”也是相同的颜色,但与你的 if 不匹配)。函数bloginfo()将回显结果,因此您无需在代码中回显 echo

<?php
if ( get_field( 'colors_text' ) ){
    // not false, so use the value from color_text
    $color =  get_field( 'colors_text' );
} else 
if ( get_field( 'colors_2' ) ){
    // color_text is not set, but colors_2 is, so use that
    $color =  get_field( 'colors_2' );
} else {
    // the default if neither color_text nor colors_2 are set
    $color = '#005b92';
}
?>
<style>
#logo {
   position:absolute;
   top: 17px;
   left: 14px;
   height: 20px;
   padding: 7px 0 0 85px;

   /* if the upper case $color is either #FFF or #FFFFFF set the background image */
   <?php if ( in_array( strtoupper( $color ), array( "#FFF", #FFFFFF" ) ) ): ?>
   background: url(<?php bloginfo("stylesheet_directory")?>/img/image_white.png) no-repeat 0 2px;
   <?php endif; ?>

   /* this was set in the PHP, so we can just echo it */
   color: <?php echo #color; ?>;
}
</style>

关于php - 我如何将背景颜色与 PHP 匹配以匹配文本颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33549344/

相关文章:

javascript - 是否可以在浏览器上编辑工具提示的字体系列?

html - 如何删除div内某个元素的填充?

php - 从远程服务器运行 PHP 代码?

java - 将ArrayList从android发送到php MySQL

php - 不同地区的锂电路线

html - 背景定位到右下角

javascript - (PHP) 如何为未选中的复选框设置默认值

javascript - 将用户从现有网站重定向到现有 WP 博客的最佳方法是什么?

mysql - 查询从mysql数据库返回未在数据库表的列中列出的表

css - Wordpress:访问CSS文件