PHP echo variable not appearing correctly - php

I have the following
<?php echo '<img src="/wp-content/themes/CAFC/images/cards/big/'.get_post_meta($post->ID, "bigcard", true).'" alt="'.the_title().'" />'; ?>
For some reason however my 'the_title' variable is appearing before my image when outputted as so...
UK Fuels Fuel Card
<img alt="" src="/wp-content/themes/CAFC/images/cards/big/ukfuels.png">
Can anybody give me an idea of where im going wrong?

This is because the_title() auto echoes the title. Try get_the_title() instead.

Try to use as:
$title = the_title();
and, use this $title in place of the_title();

Use this instead:
<?php echo '<img src="/wp-content/themes/CAFC/images/cards/big/'.get_post_meta($post->ID, "bigcard", true).'" alt="'.get_the_title().'" />'; ?>

I think it's your browser. It changes the position of attributes for you. F. ex. Firefox can fix some HTML code according to standard.

Related

Hiding empty custom field for images with Anchor CMS

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

Echoing PHP within HTML

I am currently trying to create a shopping cart for my website and I have images of products stored in a database and I want to include them within <img src> . By putting $get_row[imagesrc] within the src. I need to know the correct way to add it to the below code as I dont fully understand the ' and . tags
echo '<p>'.$get_row['name'].'<br/>'.$get_row['description'].'<br/>'.$get_row['imagesrc'].
'<br/>£'.number_format($get_row['price'],2).'Add</p>';
This should achieve what you're looking for:
echo '<p>'.$get_row['name'].'<br/>'.$get_row['description'].'<br/><img src="'.$get_row['imagesrc'].'" /><br/>£'.number_format($get_row['price'],2).'Add</p>';
The ' character defines a string literal when it is wrapped around a series of characters.
The . character is used for concatenating strings for output or storage.
echo '<p>'.$get_row['name'].'<br/>'.$get_row['description'].'<br/><img src="'.$get_row['imagesrc'].'"><br/>£'.number_format($get_row['price'],2).'Add</p>';
. concatenates two strings, and ' is wrapped around a string.
so
echo 'Hello '.'World'; // Shows Hello World
I'd split yours up to make it easier to read:
echo '<p>';
echo $get_row['name'].'<br/>';
echo $get_row['description'].'<br/>';
echo '<img src="'.$get_row['imagesrc'].'" /><br/>';
echo '£'.number_format($get_row['price'],2);
echo 'Add';
echo '</p>';
But it all looks OK.
echo '<p>'.$get_row['name'].'<br/>
<img src="'.$get_row['imagesrc'].'" alt="'.$get_row['name'].'"><br/>
<br/>£'.number_format($get_row['price'],2).'
Add</p>';`
echo '<img src="'.$get_row['imagesrc'].'">';
Try that.
A specific answer has been given:
echo '<img src="'.$get_row['imagesrc'].'">';
Nonetheless, it's worth adding that you should:
You should escape output - with htmlspecialchars() or otherwise.
echo '<img src="' . htmlspecialchars($get_row['imagesrc']) . '">';
Read the documentation on PHP Strings.
Check out this way of including PHP in your HTML. It's much easier to read and maintain. The last line in the paragraph is your image tag.
<p>
<?php echo $get_row['name']; ?><br/>
<?php echo $get_row['description']; ?><br/>
<?php echo $get_row['imagesrc']; ?><br/>
£<?php echo number_format($get_row['price'],2); ?>
Add
<img src="<?php echo $get_row['imagesrc']; ?>" />
</p>

How do I use an IF statement in PHP to decide if I should show an image or not?

I'm setting up a weather page using the wunderground.com API. Anyway, I want to do the following:
Of the variable $weather is equal to Clear, I want to display an image.
I've tried this:
if ($weather=="Clear") echo <img src="http://example.org/clear.gif" alt="Clear"/>
What do I need to do instead?
Try this
if ($weather=="Clear") echo '<img src="http://example.org/clear.gif" alt="Clear"/>';
The code you tried will render ERROR. You need to place the HTML text and any string within quotes, before you echo them.
if ($weather=="Clear") echo '<img src="http://example.org/clear.gif" alt="Clear"/>'
Remaining, there is nothing else to improve :)
if ($weather==="Clear")
echo "<img src=\"http://example.org/clear.gif\" alt=\"Clear\"/>";
echo '<img src="http://example.org/clear.gif" alt="Clear"/>';
OR
echo "<img src='http://example.org/clear.gif' alt='Clear' />";
if ($weather=="Clear") echo "<img src=\"http://example.org/clear.gif\" alt=\"Clear\"/>";
OR
if ($weather==="Clear") echo '<img src="http://example.org/clear.gif" alt="Clear"/>';
OR
if ($weather==="Clear") echo <<<ABC
<img src="http://example.org/clear.gif" alt="Clear"/>
ABC;
andsoon.
For conditionally rendering html, I often just use the php tags...
if($weather === "Clear") {?>
<img src="http://example.org/clear.gif" alt="Clear"/>
<?php}

My php variables are not being printed out.

<?php if ( is_user_logged_in() ) {
echo '<img id="visit-the-forums" src="<?php bloginfo('template_url') ?>/images/visit-the-forums.png" alt="Check out the Forums!" />'
} else {
echo '<img id="join-the-forums" src="<?php bloginfo('template_url') ?>/images/join-the-forums.png" alt="Join the Forums!" />'
}
?>
I think there is something wrong w/ the way I set up the "php bloginfo" code inside but I'm not sure how to fix it.
The code below should work for you. You had to make use of string concatenation:
<?php if ( is_user_logged_in() ) {
echo '<img id="visit-the-forums" src="' . bloginfo('template_url'). '/images/visit-the-forums.png" alt="Check out the Forums!" />'
} else {
echo '<img id="join-the-forums" src="' . bloginfo('template_url') . '/images/join-the-forums.png" alt="Join the Forums!" />'
}
?>
You have 2 problems:
Most likely this code is not
executing because you're echoing a
string that's delimited with single
quotes and inside it you've put
unescaped single quotes. (You can tell this is the case because even on this page, the syntax colouring is messed up :)
Even if you had escaped the single
quotes (e.g. <?php
bloginfo(\'template_url\') ?>) this
would not work because you're using
PHP to echo PHP code, which will then
be passed to the browser, instead of
being executed by the PHP engine.
What you need to do is to add the result of bloginfo() (or get_bloginfo(), see edit below) to the string you're outputting:
echo '<img id="visit-the-forums" src="'. bloginfo('template_url') . '/images/visit-the-forums.png" alt="Check out the Forums!" />'
(note the correct usage of single-quotes as delimiters, and the correct syntax highlighting on this page: strings are reddish, code is black)
EDIT: if bloginfo here is the WordPress function, you will want to replace it in my code above with get_bloginfo which actually returns the result rather than printing it, but your original question wasn't clear about what bloginfo is/does.

PHP echo easy problem ;)

Ok, it's Wordpress related and I know about Wordpress Stack Exchange, but I'm asking here because this is mostly a PHP question.
I want my code to display something or nothing using if statement.
The problem is I'm going to have a variable and bloginfo('template_directory') in-bulit function.
I wrote this:
<?php if (!empty($instance['example']))
echo "<li><img src="?><?php bloginfo('template_directory') ?><?php echo "/images/example.png /></li>"; ?>
It works fine until $instance['example'] is not empty, when is - it still displays the template directory link including images/example.png.
Any ideas?
I've tried " . bloginfo('template_directory') . " but doesn't seem to work.
PHP if statements that do not have braces { } will only evaluate the first line thereafter. To resolve this,
<?php if (!empty($instance['example'])) {
echo "<li><img src="?><?php bloginfo('template_directory') ?><?php echo "/images/example.png /></li>"; } ?>
Try that and see if it works for your needs. All I did was insert the braces so that your if statement spans all of your arguments.
You forgot to add the : after the if statement to make an if endif; block. Alternatively use the standard curly brackets to enclose all your commands in the if statement.
Currently it's only checking if for the first echo command.
Try this code:
<?php if (!empty($instance['example']))
echo "<li><img src=".get_bloginfo('template_directory')."/images/example.png /></li>";
?>
Try this
<?php if (!empty($instance['example'])) {
echo "<li><a href=". $example ."><img src="?><?php bloginfo('template_directory') ?>
<?php echo "/images/example.png /></a></li>";
}
?>
I'd personally use.
<?php
if( !empty( $instance['example'] ) )
echo '<li><img src="' . get_bloginfo('stylesheet_directory') . '/images/example.png" alt="" border="0" /></li>';
?>
First we add those missing attribute quotes.
Second we use the stylesheet path to ensure it points to the correct location for a child theme.
Third we call get_bloginfo to get a return value for the echo statement.
Fourth, i also added a border="0" to the image, borders aren't usually wanted for an image inside a link and also added an alt tag, because it will at least help pass HTML validation, even if you leave it empty.
Same answer as others, but with better formatted code:
<?php
if(!empty($instance['example']))
{
echo '<li><a href=' . $example . '><img src=' . bloginfo('template_directory') . '/images/example.png /></a></li>';
}
?>
Added brackets, removed unnecessary opening/closing php tags, and converted strings to single quotes since there are no variables or special characters contained within them that need processing.

Categories