<?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.
Related
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 } ;?>
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
I currently have a dynamic PHP page that displays values from a database to display a URL like this:
<a target="_blank" href="<?php echo($row['Virtual_Tour']); ?>"><?php echo($row['Virtual_Tour']); ?></a>
This works great EXCEPT when the data does not include a http://. I have not control over the data. How do I test to see if the echo starts with http:// and if not inserts that as well to guarantee the URL is properly formatted?
if(substr($row['Virtual_Tour'],0,7)=="http://")
{
//starts with http://, no formatting needed
}
else
{
$row['Virtual_Tour']="http://".$row['Virtual_Tour'];
}
You can just remove the exist http://, if it exists, and put a newone like below code
<a target="_blank" href="<?php echo "http://".str_replace("http://","",$row['Virtual_Tour']); ?>"><?php echo "http://".str_replace("http://","",( $row['Virtual_Tour']); ?></a>
you can do this also with https:// like
<a target="_blank" href="<?php echo "http://".str_replace("https://","", str_replace("http://","",$row['Virtual_Tour'])); ?>"><?php echo "http://". str_replace("https://","",str_replace("http://","",( $row['Virtual_Tour'])); ?></a>
If I get it correctly, if your url does not contain http:// you want to add it, since some answers provide solution to show it only like this:
http://something.com.
If I am right, you could use this:
$string = "something.com";
$string = substr($string,0,6) != 'http://' ? "http://".$string : $string;
echo $string;
working example
I'm trying to implement the following guide to my custom wordpress theme http://tomsbigbox.com/wordpress-load-more-posts-on-page-scroll/
Unfortunately I have problems to include metabox and term values into the PHP variable.
How can I add/echo a variable inside of another variable? I hope I'm clear enough.
<?php
// .. wordpress function ... (have_posts()) : while (have_posts()) : the_post();
// Metabox
$email = get_post_meta( $post->ID, '_rsd_email', true );
// Taxonomy Term
$city = get_rsdt_terms('city');
$item = '<li>
<a id="itemn-'. get_the_ID() .'" href="'. get_permalink($post->ID) .'" title="'. get_the_title() .'">
<span>
<div class="role sixcol">
<h3>'. get_the_title() .'</h3>
<h4>'. $email .'</h4>
<span></span>
</div>
<div class="location threecol"><span>'. $city .'</span></div>
</span>
</a>
</li>';
// wordpress .....
?>
The simplest answer to this is to add some debugging statements to your script to output the contents of the variables. The variables are fully accessible within the scope of the script unless they are encapsulated within a function or class.
The most commonly used statement is to pass the variable into the print_r() function. So you would do something like "print_r($city);" to get the contents of that.
I, personally, like to use a slightly more extensive version of that command that ensures it outputs the results in a cleaner, more easily read fashion. Here is what I normally write.
echo "\n<br>\n<pre>" . print_r($var, true) . "</pre>\n<br>\n";
The tags ensure that the browser renders the spaces and line breaks from the output. The "\n" and that you see makes sure that it does both browser rendered line breaks before and after and source output line breaks.
You can do this with pretty much any variable type. The only times I have run into an issue with the output of a variable is if the variable is actually (or contains) a resource. An example of that is when outputting SimpleXML variables.