My WordPress options panel has a section where the user can paste their logo’s URL to show up in the header. If the input is blank, I want the Blog’s title to show up instead on my header. The ID of the input is “nl_logo”, so I added an if statement in my header.
<?php if ("nl_logo") { ?>
<img src="<?php echo get_option('nl_logo'); ?>">
<?php } else { ?>
<h1><a href="'get_site_url()' ?>"><?php bloginfo('name'); ?></a></h1>
<?php } ?>
The first part of the if statement works. However, anything below else doesn’t work when I have no URL saved in my input. So, if the input is empty, how do I display something else with PHP? Or is there a different and better way to do this? For example, creating a function and calling the results to display with a simple line of PHP?
Try this… also try to understand it.
Keeping with the established coding style:
That should atleast be valid PHP now. I don’t know if the functions you are using are correct, but if they are this should work.
Here is a cleaner way to do it…
Option three because I’m feeling “teachy” using a ternary. Probably a little long for this to be the best choice, but it is another option.
Note I switched the if / else because I’m using empty and it just feels cleaner to do it this way instead of using !empty()
Should be
You could try the
empty
method to test if a string is empty. In context:This does not make sense:
You’re basically saying if the string exists then proceed, which it does, of course.
It makes more sense to take the input string:
As an example, doesn’t really matter as long as you know how you got the value into
$input
.Now, you can use the ternary operator to determine whether you want to use the user’s input or if you want to use the default title:
Depending on what your functions do.. if get_options returns a string then this will work.
Anyway, don’t mis PHP and HTML, it will make things complicated and you’ll be locked into bad design.
Note:
Make sure to check if the input is actually received, using
isset
.You are testing for string “nl_logo” to be true. That returns always true.
try changing your code like this:
Don’t close the php tags.
Whatever html you need to write, just write it as echo statements within one big php block.
This should fix your problem