Ok, it’s WordPress related and I know about WordPress Stack Exchange, but I’m asking here because this is mostly a PHP question.
I want my code to display something or nothing using if statement.
The problem is I’m going to have a variable and bloginfo(‘template_directory’) in-bulit function.
I wrote this:
<?php if (!empty($instance['example']))
echo "<li><a href=". $example ."><img src="?><?php bloginfo('template_directory') ?><?php echo "/images/example.png /></a></li>"; ?>
It works fine until $instance[‘example’] is not empty, when is – it still displays the template directory link including images/example.png.
Any ideas?
I’ve tried ” . bloginfo(‘template_directory’) . ” but doesn’t seem to work.
PHP if statements that do not have braces { } will only evaluate the first line thereafter. To resolve this,
Try that and see if it works for your needs. All I did was insert the braces so that your if statement spans all of your arguments.
You forgot to add the
:
after the if statement to make anif endif;
block. Alternatively use the standard curly brackets to enclose all your commands in the if statement.Currently it’s only checking
if
for the firstecho
command.Try this code:
Try this
I’d personally use.
First we add those missing attribute quotes.
Second we use the stylesheet path to ensure it points to the correct location for a child theme.
Third we call
get_bloginfo
to get a return value for the echo statement.Fourth, i also added a
border="0"
to the image, borders aren’t usually wanted for an image inside a link and also added an alt tag, because it will at least help pass HTML validation, even if you leave it empty.Same answer as others, but with better formatted code:
Added brackets, removed unnecessary opening/closing php tags, and converted strings to single quotes since there are no variables or special characters contained within them that need processing.