I have a straightforward if statement to evaluate whether or not an image is set to appear as the logo on a WordPress build. If it returns empty/false, it displays a default image whose location is set as an absolute value.
The problem is when an image ISN'T set, the else statement is failing. I'm not receiving an error, but the code returned is simply an image tag without any source i.e. "< img src >".
Here is the statement:
<?php
$logo = $wp_options['header_logo'];
if(isset($logo) && ($logo !='')) { ?>
<img src="<?php echo $logo['url']; ?>">
<?php } else { ?>
<img src="wp-content/themes/wpdev/images/logo.png">
<?php } ;?>
I guess that if statement is failing, because you are treating $logo variable as a string.
It seems you're using $logo variable as an array, if you want to check out if it is empty you can use is_null() function of PHP.
By the way, we can't understand your problem this way, you should be more specific. Share your error or warning messages, the way it's behaving, etc..
Found a solution to this by specifically referring to the url of the image array when the variable $logo was defined. This way, the IF is evaluating the URL of the image.
The problem seemed to be that the initial IF was being evaluated as TRUE but obviously not returning the URL as this wasn't originally specified in $logo. By looking for the URL in the 'header_logo' array, it corrected the problem.
<?php
$logo = $wp_options['header_logo']['url'];
if(isset($logo) && ($logo !='')) { ?>
<img src="<?php echo $logo; ?>">
<?php } else { ?>
<img src="<!-- Image Location -->">
<?php } ;?>
Related
I am trying to display an image on my wordpress-theme depending on which author wrote the page.
Therefore I am doing this:
<img src="<?php $autor=the_author_meta('display_name');
if (strpos($autor,'foo') !== false)
{echo esc_url(get_template_directory_uri() ); ?>/bilder/foo.jpg" />
<?php } else {echo esc_url( get_template_directory_uri() ); }
?>/bilder/fun.jpg" />
But this is throwing several errors. It's a) not pointing to the right path by not using get_template_directory_uri() at all. And it's b) adding me the Authorname within the url now like this
http://domain.com/Author%Name/bilder/fun.jpg
What am I doing wrong? And I don't want to use a plugin therefore - but thanks ;)
the_author_meta() is set to output the result, you want to just return it. So instead change it to get_the_author_meta().
Also I've tidied up your code, as I think there were some errors in it.
<img src="<?php $autor = get_the_author_meta('display_name');
if (strpos($autor,'foo') !== false) {
echo esc_url(get_template_directory_uri())."/bilder/foo.jpg";
} else {
echo esc_url(get_template_directory_uri())."/bilder/fun.jpg";
} ?>" />
When I replace foo with my user name, the image path is displayed as /bilder/foo.jpg, when I change it back to anything that isn't my username, it's shown as /bilder/fun.jpg, so I believe it works fine. The template URL appears fine too.
<img src="http://test.local/wp-content/themes/test/bilder/fun.jpg">
The explanation for this line is that Anchor doesn't display the image from the custom field correctly. So I had to add the missing part of the path. It works just fine, but now the problem is that I get horrendous icons on Safari when there is no image fetched in the field image:
<?php echo "<img src='http://www.firstpartoftheurl/" . article_custom_field('image') . "' alt=' ' height='300' >"; ?>
May I show this line only when the custom field is populated?
And how can I hide custom fields when they are empty?
This is how I solved it:
<?php
$image = article_custom_field('image');
if (!empty($image)) {
echo "<img src= 'http://www.firstpartoftheurl".article_custom_field('image')."' alt='blabla ".article_title()."'>"; //if there is image show it
} else {
//if not do nothing
} ?>
I hope it helps. It works for me but if someone has a better solution, please let us know.
Shorter still:
<?php if (article_custom_field('featured-img')) :?>
<img src="<?php echo article_custom_field('featured-img')?>" alt="<?php echo article_title(); ?>" />
<?php endif; ?>
If article_custom_field() is not returning the full URL, there might be something up with your server configuration, because this has always worked for me. Otherwise, just prepend $_SERVER['SERVER_NAME']... this is better than hard-coding the URL.
There is documentation on the function here which also explains how to use its fallback: http://anchorcms.com/docs/function-reference/articles
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><?php bloginfo('name'); ?></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:
<?php $nlLogo = get_option('nl_logo'); ?>
<?php if (empty($nlLogo)) { ?>
<h1><?php echo(bloginfo('name')); ?></h1>
<?php } else { ?>
<img src="<?php echo($nlLogo); ?>">
<?php } ?>
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...
<?php
$nlLogo = get_option('nl_logo');
if (empty($nlLogo)) {
echo('<h1>'.bloginfo('name').'</h1>');
} else {
echo('<img src="'.$nlLogo.'">');
}
?>
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.
<?php
$nlLogo = get_option('nl_logo');
echo(empty($nlLogo) ? '<h1>'.bloginfo('name').'</h1>' : '<img src="'.$nlLogo.'">');
?>
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()
<h1><?php bloginfo('name'); ?></h1>
Should be
<h1><?php echo bloginfo('name'); ?></h1>
You could try the empty method to test if a string is empty. In context:
if(!empty($nl_logo)) {
// stuff
} else {
// other stuff
}
This does not make sense:
if ("nl_logo")
You're basically saying if the string exists then proceed, which it does, of course.
It makes more sense to take the input string:
$input = $_POST["postedInput"];
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:
$img = $input == "" ? bloginfo($input) : get_option($input);
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:
<?php
$nl_logo = get_option('nl_logo');
if (!empty($nl_logo)):
?>
<img src="<?php echo $nl_logo; ?>">
<?php else: ?>
<h1><?php bloginfo('name'); ?></h1>
<?php endif; ?>
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
I have been fighting with the following code and variants, but have not been able to come up with anything that works. The idea is that I detect if the variable $largeimg6 (the URL of an image) is empty or not (whether an image has been loaded) and if not, load the value of a default variable ($defaultimage) which holds the URL of a default image..
<?php if(!empty($largeimg6)) : ?>
<img class="rpPicture" id="rpPicture<?php echo $key; ?>" onload="showPicture(this, <?php echo SHOW_PICTURES; ?>)" width="60" height="60" src="<?php echo $largeimg6; ?>" alt="Buy CD!" title="Buy CD!" />
<?php endif; ?>
The problem is that I do not know where to add the arguement for $largeimg6 being empty and therefore adding:
$largeimg6 = $defaultimage
Any help would be appreciated.
Thanks.
You can try this before that if statement:
<?php $largeimg6 = $largeimg6 != '' ? $largeimg6 : $defaultimage; ?>
The != '' comparision may be change according to your need to isset(), strlen(), is_null()(as Ville Rouhiainen suggested) or whatever.
You can use the alternative (and trim as already suggested):
$largeimg6 = trim($largeimg6);
if (isset($largeimg6)&&strlen($largeimg6)>0)
but you'll probably be doing better by filtering the url:
$largeimg6 = trim($largeimg6);
if(filter_var($largeimg6, FILTER_VALIDATE_URL))
More info: http://php.net/manual/es/book.filter.php
Solution
This answer assumes that you have a variable $largeimg6 which is either going to be set to a url or be empty.
That being the case it's a fairly simple fix. You need to remove the if statement entirely and replace your:
<?php echo $largeimg6; ?>
with:
<?php echo (empty($largeimg6)) ? '/default/image.jpg' : $largeimg6; ?>
Explanation
The above is equivalent to:
if(empty($largeimg6)){
echo '/default/image.jpg';
}
else{
echo $largeimg6;
}
But in the form:
(IF) ? THEN : ELSE;
I have a Magento store and I would like to replace the src value of an img based on whether the referring URL contains a specific querystring or querystring value (adnetwork=as) and if not simply leave the image as it is.
Is this simple enough to do, I've searched everywhere for the answer, and even asked on various forums to no avail.
Here's what I have so far, seems like the correct syntax, just Firebug reports that it is unable to load the image URL, and it simply shows "". The paths are correct and have been tested.
<?php
// Path for default image source
$logo = $this->getSkinUrl('images/logo.gif');
// Get the referrer url from the $_SERVER array, or false if it's empty
$referrer = empty($_SERVER['HTTP_REFERER']) ? false : $_SERVER['HTTP_REFERER'];
// If the referrer exists
if($referrer) {
// parse the url for the query
$query_str = parse_url($referrer, PHP_URL_QUERY);
// If there's a query string
if(!empty($query_str)) {
// Parse it and put the array into $components
parse_str($query_str, $components);
// If the "adnetwork" component is set
if(!empty($components['adnetwork'])) {
// Set the $logo var to the adnetwork image source
$logo = $this->getSkinUrl('images/logo-no-number.gif');
}
}
}
?>
<div class="header">
<img class="shop24" src="<?php echo $this->getSkinUrl('images/shop-24.gif') ?>" alt="Shop Online 24 Hours a Day"/>
<div class="shopping-bag"><?php echo $this->getChildHtml('topcart')?></div>
<div id="fplogo"><img src="<?php echo $logo; ?>" alt="Sale Basins - Clickbasin.co.uk" /></div>
<img src="<?php echo $this->getSkinUrl('images/side_logo_promo.gif') ?>" alt="Promotion" class="side-logo-promo"/>
<?php echo $this->getChildHtml('topMenu') ?>
<?php //<?php echo $this->getLogoSrc() ?>
</div>
I hope you can help guys. Thanks.
Shouldn't you be simply echoing $logo? you are assigning the image to $logo but you never set anything to $logo.