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">
Related
<?php
// query db once for permalink
$permalink = urlencode(get_permalink());
?>
<a href='https://www.facebook.com/sharer/sharer.php?u=<?php echo $permalink; ?>' onclick='javascript:return (function(){window.open("https://www.facebook.com/sharer/sharer.php?u=<?php echo $permalink; ?>", "MsgWindow", "width=600, height=600, scrollbars=yes", false); return false;})();'>
<img onmouseover="this.style.opacity='0.8'" onmouseout="this.style.opacity='1'" style="margin-right:5px; width:40px; height:40px" src="data:image/png;base64,..." alt="Facebook Share" />
</a>
The above code executes in an Ad Inserter block but returns the wrong permalink.
I have no other plugins installed for debugging purposes so that means no caching enabled.
The code executes w/o error but doesn't return the correct URL.
What could be wrong?
Try this:
get_the_permalink()
https://developer.wordpress.org/reference/functions/get_the_permalink/
And I don't think you have to urlencode() it.
Edit:
It works only in the loop without any parameters. Outside the loop you have to pass the Post ID to the function.
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 } ;?>
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 Conditional PHP is not working and i dont know why. Can you help me?
Here the Code:
<?php
if( is_page(array('5279','4945') ))
{
echo '<img src="http://www.example.de/logo_with_text.png">';
}
elseif( is_page(array('5656','5668','5672','5677','5682','5690','5735','5738','5741','5744','5749','5752')))
{
echo '<img src="http://www.example.de/logo_with_text_and_icon.png">';
}
else
{
echo '<img src="http://www.example.de/logo_without_text.png">';
}
?>
But i need it in this way... (letters are the Pages ids)
For pages a,b,c,d i need a own logo
For pages e,f,g,h i need a own logo
For pages j,k,l,m i need a own logo
and for Page n,o,p,q i need a own logo
Try this code, literally just tidied it slightly and removed the quotation marks round the page numbers.
<?php
if(is_page(array(5279,4945) ))
{
echo '<img src="http://www.example.de/logo_with_text.png">';
}
elseif(is_page(array(5656,5668,5672,5677,5682,5690,5735,5738,5741,5744,5749,5752)))
{
echo '<img src="http://www.example.de/logo_with_text_and_icon.png">';
}
else
{
echo '<img src="http://www.example.de/logo_without_text.png">';
}
?>
Just make sure you have the right pages. To do this go to pages in the dashboard and hover over the link to the page you want, you should see it shows the id number in the url, ie post=100. This is the id for that page and the one you will need to use. Using the id means that regardless of the page name, this code will always work for you.
An extreme test would also be using the below instead of the else
elseif(!is_page(array(5279,4945,5656,5668,5672,5677,5682,5690,5735,5738,5741,5744,5749,5752)))
{
echo '<img src="http://www.example.de/logo_without_text.png">';
}
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.