Code outputting html instead of an image? - php

Here is the code that I am referring to:
<?php if ( is_archive() ) { echo '<img src="'.bloginfo('template_url').'/images/test.png" />'; }?>
This is what the code outputs: http://site.com/wp-content/themes/themename
I'd like it to output the actual image in the code. What part of this did I overlook?

bloginfo() doesn't output the string. It echo's it directly to output stream.
So, the code should be:
<?php if ( is_archive() ) { ?>
<img src="<?php bloginfo('template_url'); ?>/images/test.png" />';
<?php } ?>
Or else, you can use get_bloginfo() :
<?php if ( is_archive() ) { echo '<img src="'.get_bloginfo('template_url').'/images/test.png" />'; }?>

Have you tried this:
$template_url = get_bloginfo('template_url');
<?php if ( is_archive() ) { echo '<img src="'.$template_url.'/images/test.png" />'; } ?>

Related

expecting statement -php syntax | if-else

<?php if ( wp_get_attachment_image($item['image']['id'], 'home-post-thumbnail') !='' ) { ?>
<?php echo wp_get_attachment_image( $item['image']['id'], 'home-post-thumbnail' ); } ?>
<?php else {
?> <img class="img-responsive img-whp" src="<?php echo plugin_dir_url( __FILE__ ) . '/6-450x450.jpg'; ?>">
<?php } ?>
I get critical error.phpstorms says that expecting statements.I couldnt find a solution
You cannot "interrupt" the PHP code after the closing { of the if and before the else keyword.
Cleaned up version:
<?php if (wp_get_attachment_image($item['image']['id'], 'home-post-thumbnail') != '') {
echo wp_get_attachment_image($item['image']['id'], 'home-post-thumbnail');
} else {
?><img class="img-responsive img-whp" src="<?php echo plugin_dir_url( __FILE__ ) . '/6-450x450.jpg'; ?>"><?php
} ?>
To avoid such errors, try learning and following a coding standard, e.g. PSR-12.
Since you are working with WordPress, here is WP's coding standard for PHP: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/php/

Wordpress & PHP to check if custom field exists, and if not replace with a random string

I'm trying to create a function that checks for the existence of a post_thumbnail() for a post in Wordpress and sets an element's background image appropriately. Here's what I've tried:
<div class="header-cover-image" style="background-image: url(
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail('full');
} else { echo get_template_directory_uri() . '/assets/img/article-overlay-1.jpg' }?>
">
</div>
This currently crashes/causes the page to render nothing at all. I'm so confused about when and where to use quotation marks when using PHP within HTML markup!
Any ideas how I can make this work?
Try bloginfo ;
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail('full');
} else { echo get_bloginfo('stylesheet_directory') . '/assets/img/article-overlay-1.jpg'; }?>
Cheers!
There was a problem with trying to echo the Wordpress function and append the string simultaneously. Fixed it by doing:
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail('full');
} else { $uri = get_template_directory_uri(); echo $uri . '/assets/img/article-overlay-1.jpg'; }?>
">

Conditional div class doesn't echo, only the variable within does

Is there any reason why this is happening due to the following code? All that gets displayed is the variable , i.e the image.
<?php $featured_image = the_post_thumbnail();?>
<?php if (is_page(7) || is_page(12))
echo '<div class="featured_image">' . $featured_image . '</div>'
?>
<?php
$featured_image = the_post_thumbnail();
if (is_page(7) || is_page(12)) {
echo '<div class="featured_image">' . $featured_image . '</div>';
}
?>
The function itself echos the img: http://codex.wordpress.org/Function_Reference/the_post_thumbnail
try:
<?php
if ( has_post_thumbnail() && (is_page(7) || is_page(12))) {
echo '<div class="featured_image">';
the_post_thumbnail();
echo '</div>';
}
?>
What you are seeing is the return value of the echo function.

unsure how to add bloginfo reference to images inside wordpress loop

I want to use the <?php bloginfo('stylesheet_directory'); ?> inside my wordpress loop to reference an image but unsure how to do this. My image code is as follows:
<?php
if (is_category('Events')) {
echo '<img src="http://localhost/mmj/wp-content/themes/child-theme/img/live-banner.jpg" class="live-holder-img" />';
} else if (is_category('News')) {
echo '<img src="http://localhost/mmj/wp-content/themes/child-theme/img/live-banner.jpg" class="live-holder-img" />';
} else {
echo '<img src="" class="default" />';
} ?>
I would rather replace http://localhost/mmj/wp-content/themes/child-theme with <?php bloginfo('stylesheet_directory'); ?> but Im aware that I cant include <?php inside <?php so I was wondering how I could do this?
You can use like this :
echo '<img src="'.get_bloginfo('stylesheet_directory').'/img/live-banner.jpg" class="live-holder-img" />';
Full code :
<?php
if (is_category('Events')) {
echo '<img src="'.get_bloginfo('stylesheet_directory').'/img/live-banner.jpg" class="live-holder-img" />';
} else if (is_category('News')) {
echo '<img src="'.get_bloginfo('stylesheet_directory').'/img/live-banner.jpg" class="live-holder-img" />';
} else {
echo '<img src="" class="default" />';
} ?>
In shortly,
I'm using get_bloginfo() instead of bloginfo() for getting stylesheet directory, not printing out. And then using it like this :
echo 'Some strings here ' . get_bloginfo() . ' another strings';

Am I missing an escape for one of these quotes?

I'm not entirely sure why this isn't working. But this piece of code does not work.
<?php
foreach ( $gallery_ids as $gallery ) {
echo '<div class="tgallery" rel="'.$gallery['gid'].'"><?php echo do_shortcode("[nggallery id='.$gallery['gid'].']"); ?></div>';
}
?>
I was guessing that maybe I'm putting the wrong quotes in the wrong place.
All the parts seperately work, as in :
I can display the 'gid' value with echo $gallery['gid']
I can make the div tags appear with the appropriate rel
I can, by itself, make <?php echo do_shortcode("[nggallery id=3]"); ?> work.
I just can't make the entire thing appear together.
You're mixing interpolated php and html, placing "<?php echo" inside what's already php.
<div class="tgallery" rel="<?php echo $gallery['gid'];?>">
<?php echo do_shortcode('[nggallery id="'.$gallery['gid'].'"]'); ?>
</div>
Why you put <?php ?> inside your echo ?
<?php
foreach ( $gallery_ids as $gallery )
{
echo '<div class="tgallery" rel="'.$gallery['gid'].'">'.do_shortcode('[nggallery id='.$gallery['gid'].']').'</div>';
}
?>
The issue
Pick either string concatenation or opening/closing PHP for HTML. You cannot combine both as you have done above.
echo '<div class="tgallery" rel="'.$gallery['gid'].'">
<?php echo do_shortcode("[nggallery id='.$gallery['gid'].']"); ?>
</div>';
The second line of code above does not belong inside a string as code in between the <?php ... ?> will not be parsed by PHP when it is contained in a string.
Solutions
Concatenation
I have fixed your code to use concatenation below:
foreach ( $gallery_ids as $gallery ) {
$shortcode = do_shortcode("[nggallery id={$gallery['gid']}]");
echo '<div class="tgallery" rel="' . $gallery['gid'] . '">' . $shortcode . '</div>';
}
Opening and closing PHP
This is how you would do it using PHP "templating":
<?php foreach($gallery_ids as $gallery ): ?>
<div class="tgallery" rel="<?php echo $gallery['gid']; ?>">
<?php echo do_shortcode("[nggallery id={$gallery['gid']}]"); ?>
</div>
<?php endforeach; ?>
You are already "in" php, so your opening tag is causing the problem:
<?php echo do_shortcode("[nggallery id='.$gallery['gid'].']"); ?>
It should be something like:
echo '<div class="tgallery" rel="'.$gallery['gid'].'">' . do_shortcode('[nggallery id='.$gallery['gid'].']') . '</div>';
<?php
foreach ( $gallery_ids as $gallery ) {
echo "<div class=\"tgallery\" rel=\"{$gallery["gid"]}\">". do_shortcode("[nggallery id=\"{$gallery["gid"]}\"]") ."</div>";
}
?>
try this....
<?php
foreach ( $gallery_ids as $gallery ) {
echo '<div class="tgallery" rel="'.$gallery['gid'].'">'.do_shortcode("[nggallery id=".$gallery['gid']."]").'</div>';
}
?>
you have inside a php statment
Try Notepad then it will colour code your php code, so you can clearly see what quotes etc you have wrong

Categories