I am a beginner at using PHP! I know there have been questions like this before, however, they do not work for me the way I want them too.
Here's what's going on: I have multiple background images that I implement using css and php. Each of those images, so far, are black, white, and blue. I want to be able to write some php...again I'm a beginner so please excuse my ignorance... that changes the background image to either one of those listed above to match the color of text with it.
Some things to possibly note: This is being done on a php file. The file also corresponds to a wordpress(ACF) plugin. I can change the text color on this wordpress plugin. I have tried using github, I have looked at previous answers on this site and google, however, I believe my lack of knowledge of php and javascript have left me more confused. I think what I am trying to achieve can be done with simple if else statements. Also it's worth noting that I want an answer in php only, not javascript or jquery or github (as these answers only confuse me more). Please help and thank you.
Here's a snip-it of the relevant code I have written so far.
#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';?>;
}
I'm not sure if I understand your question correctly, but if I do, the following ought to deliver what you need it to:
<?php
$styledir = "stylesheet_directory";
$image = "image_default.png";
$color = "color: #005b92;";
if ($color_text) {
$color = "color: ".$color_text.";";
switch ($color_text) {
case ("#FFF") : $image = "image_white.png"; break;
case ("#000") : $image = "image_black.png"; break;
case ("#00F") : $image = "image_blue.png"; break;
default: $image = "image_default.png";
}
}
$background = "background: url('";
$background .= bloginfo($styledir);
$background .= "/img/";
$background .= $image;
$background .= "'); ";
?>
#logo{
position:absolute;
top: 17px;
left: 14px;
height: 20px;
padding: 7px 0 0 85px;
<?php echo $background; ?>
<?php echo $color; ?>
}
Since you have a somewhat complex if statement, it's not recommended to write it as a ternary operator (condition ? true : false
), rather it would be better to use an if statement.
In your example you are using the variable names $colors_text
and $colors_2
, however you haven't populated these with the ACF field values. You can do this with the get_field()
function to return the value, and the_field()
to echo the value (similar to WordPress functions in the loop: the_title()
and get_the_title()
for example). You can use get_field()
in an if statement because it will return false
if the field isn't set.
You also have some syntax errors. While it's true you can't embed PHP tags within PHP tags, your if statement for the "background" is generally incorrect. You have if $color_text = ("#FFF")
but should have if ($color_text == "#FFF")
- the left paren is in the wrong place and you are using =
(assignment) instead of ==
(logical or). I think you are trying to only set the background image if the color is white (or more precisely "#FFF", since "#FFFFFF" is also the same color but wouldn't match your if). The function bloginfo()
will echo the results, so you don't need to echo
in your code.
<?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>