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
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 } ;?>
How do I get my image id to be echoed in another page?
Page Portfolio:
[ image one ]
[ image two ]
[ image one ] has id of image1. When clicking on image1, it will direct to a new page to display in a larger size.
so far, here is my code.
Page Portfolio
<img src="image1.jpg"/>
<img src="image2.jpg"/>
view.php
<?php $id=$_GET['id']; ?>
<?php if($id == 'image1'){
echo '<img src="orange.jpg"/>';} ?>
<?php if($id == 'image2'){
echo '<img src="milk.jpg"/>';} ?>
It is acceptable if I use this code for a few pictures, but I am going to use it for a lot of pictures. Any suggestion or tips? Is it possible to echo based on the id of the images?
Any help is appreciated. Thank you for your time.
I'm really sorry that I forgot to mention that I am not getting the image from database. Please take a look at the view.php I've edited above. I'm not sure if I explain myself clear enough.
Try this
<?php $id=$_GET['id'];
echo '<img src="'.$id.'.jpg"/>';
?>
You could use the GET value as part of the src attribute, like so:
<?php
echo '<img src="' . $_GET['id'] . '".jpg" />';
?>
But note that this requires every image to have the same jpeg extension.
You CAN'T echo img src=... in the php because you already doing this in the original HTML as a source, you need to print the picture.
//phpFile.php
<?php echo "<img src..">;
will be parsed as an HTML document, so in the html
the source will be an html document and NOT an Image.
you need to replace img to iframe or
use this in the php
$file = $_GET['id']; //or full path to file
$type = 'image/jpeg';
header('Content-Type:'.$type);
header('Content-Length: ' . filesize($file));
readfile($file);
While it might be a bit overkill and the other answers having a more straight approach, I figured I'd give you this option as well. (This is for your view.php file of course :) )
<?php
$id = $_GET['id'];
$validImages = array
(
'image1.jpg',
'image2.jpg',
'image3.jpg'
);
if(in_array($id,$validImages)){
<-- DISPLAY IMAGE HERE -->
}else{
die('Invalid image');
}
And if you desire #Daniel Krom's edition with the header(); part it's just a simple matter of putting it inside of the if(){} clause instead of the echo :)
Thank you for all your suggestions! I manage to get it work using the following codes. :) - Nurul
Page Portfolio
<img src="image1.jpg" id="image1"/>
<img src="image2.jpg" id="image2"/>
view.php
<?php $id=$_GET['id']; ?>
<img src="<?php echo $id; ?>.jpg"/>
(if and only if the extension of all images are the same just like FDekker said.)
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">
I am making options in the 'customization api' for Wordpress. However for things such as the logo image, if no image has been uploaded (default value is blank) i want it to display the title of the Wordpress site. I have searched google a lot, but I just cant seem to get it to work.
I know the PHP code for the Wordpress site title, but I can't get the php 'if' and 'else' to work.
current code:
<img src="<?php echo get_theme_mod('logo_image'); ?>" alt="<?php bloginfo('name'); ?>"/>
..obviously this is just the option to upload the image, which WORKS, but I want it to display the site title if the default value is empty.
Thank you!
I don't know how you are setting the theme mod. I.e. are you using 'set_theme_mod' ?
But assuming you are complying to WordPress standards the code snippet below should be what you are seeking.
if (!empty(get_theme_mod('logo_image'))) {
echo "<img src='" . get_theme_mod('logo_image') . "' title='" . bloginfo('name') ."'>"
} else {
echo "<h2>" . bloginfo('name') . "</h2>";
}
Hope this helps you out,
Dave.
Can you try this?
<img src="<?php if (!isset(get_theme_mod['logo_image'])) {
echo ('Your custom image');
}
else{
echo get_theme_mod('logo_image');
}
?>" alt="<?php bloginfo('name'); ?
>"/>
Using the latest version of Wordpress & Buddypress, I am trying to display a certain custom profile field in the WP header.php. I'm terrible with PHP, but this is what I have:
<?php
global $bp;
$the_user_id = $bp->loggedin_user->userdata->ID;
if (function_exists('bp_get_profile_field_data')) {
$bp_gamertag = bp_get_profile_field_data('field=Gamertag&user_id='.bp_loggedin_user_id());
if ($bp_gamertag) {
echo '<img src="http://avatar.xboxlive.com/avatar/$bp_gamertag/avatar-body.png" alt=""/>';
}
else
echo '<img src="http://avatar.xboxlive.com/avatar/xbox/avatar-body.png" alt=""/>';
}
?>
I can't quite figure out why it isn't working. The source shows the variable still in the URL.
Also, I don't think I need the $user_user_id variable, as it isn't really being used, do I? I'm following the instructions in this topic: http://buddypress.org/support/topic/how-to-get-user-profile-data/
Try :
echo "<img src='http://avatar.xboxlive.com/avatar/$bp_gamertag/avatar-body.png' alt='' />";
Notice the use of single vs. double quotes.