PHP variable with string - php

I'm trying to get an image using the following
$html ='<img alt="" src="'.bloginfo('stylesheet_directory').'/images/my-image.png">';
When I return $html the path from bloginfo('stylesheet_directory') shows in the top portion of the page instead in the src attribute of the image.

You should use use get_stylesheet_directory_uri() instead. And you are missing a quote (as CyberJunkie pointed out). Try this:
$img_path = get_stylesheet_directory_uri() . '/images/my-image.png';
$html = '<img alt="" src="' . $img_path . '" />';
There is also a sample in the wordpress codex:
<img src="<?php echo get_stylesheet_directory_uri() ?>/images/aternus.png" alt="" title="" width="" height="" />

bloginfo will automatically echo out the directory you are requesting, and has no return value. The output happens when you are building your string, not whenever you are echoing the $html variable.
I think the function you are looking for is get_stylesheet_directory_uri
Example usage of bloginfo (notice no echo):
<?php bloginfo('name'); ?>
Example usage of get_stylesheet_directory_uri:
<?php echo get_stylesheet_directory_uri(); ?>

Related

How to add "../" into a PHP echo img src

In my code I show some images in this way:
...
<?php echo '<img src="' .$imageone. '" alt="" /> '?></a></div>
<?php echo '<img src="' .$imagetwo. '" alt="" /> '?></a></div>
...
where $imageone, $imagetwo have path from Mysql database
e.g.
$imagesone = "images/exposition/02-05-2017-11-28-00-foto 2_b.JPG";
This code is working in the same place where I have my images folder, but now I need to put the same code in a subfolder page and I'd like to use the same image.
So, I need to put dinamically "../" before my varible, something like this:
...
<?php echo '<img src="../' .$imageone. '" alt="" /> '?></a></div>
<?php echo '<img src="../' .$imagetwo. '" alt="" /> '?></a></div>
...
but it's not working.
Any suggestion?
EDIT This is my solution:
if ($images1 != ""){
$imageone = "../".$images1;
}
if ($images2 != ""){
$imagetwo = "../".$images2;
}
in this way I fixed my code and it's working!
Since you're using double quotation marks after src, you can just write down <img src="../$imageone" alt="" />
There's no need to use simple quotation marks to write the variable because the double quotation marks gets the variable number.

Link to be displayed as imgsrc

I have a code here that outputs a image link like http://img.domain.com/2515.jpg
<?php echo IMG_URL . $code . ".jpg" ?>
But i want to make it print this entire thing <img src="http://img.domain.com/2515.jpg" alt="" title="Created by domain.com" />
How can i format that php string <?php echo IMG_URL . $code . ".jpg" ?>
to include that entire img src link?
I am trying to fit it in this html code below
<li><a class="linkInsert" data-value="<?php echo IMG_URL . $code . ".jpg" ?>">Direct Link (email & IM)</a></li>
Update:
i figured it out below with just using '
<li><a class="linkInsert" data-value='<img src="<?php echo IMG_URL . $code ?>.jpg" alt="">'>HTML Image (websites / blogs)</a></li>
Try this,
<?php echo '<img src="'.IMG_URL.$code.'.jpg" alt="" title="Created by domain.com" />' ;?>
You just have to use single quotes and double quotes alternatively.
PHP can be embedded inside HTML:
<img src="<?php echo IMG_URL . $code ?>.jpg" alt="">

How to pass a string from PHP into an html tag?

I have a string, an img url, in a php block called $str. And I want to set that string as the img src in an img src but its not working.
<img src="<?php $str ?>" height="50px" width="50px">
how else can i set the src to the $str string?
<img src="<?php echo $str;?>" height="50px" width="50px">
Well, I found correct answer for this problem. Nothing works well from above answers, that codes only print out source string to html page on site.
This works for me (I have my function that return source string of picture):
require_once("../../my_coded_php_functions.php");
<?php echo '<img src="' . getSourcePathOfImage() . '" />' ?>
This site(article) helped me to find and understand solution:
http://php.net/manual/en/faq.html.php
<?php echo '<img src="'.$str.'" height="50px" width="50px">' ?>
Other ways, although is not recommended. You may try :
<img src="<?=$str?>" height="50px" width="50px">
this will work, only if (in php.ini)
short_open_tag = On

Wordpress advanced excerpt - use image in 'read more' link, using get bloginfo

I'm trying to output a custom image in my excerpt 'read more' links.
Like so...
I am using the advanced exceprt plugin because it so much customizable for my needs.
This is how I've tried to output the excerpt with the image in the readmore link...
<?php the_advanced_excerpt('length=120&use_words=0&no_custom=0&ellipsis=%26hellip;&read_more=read more <img src"' . get_bloginfo('template_url') . '"/images/readmore-arrow.png" alt="" />&add_link=1'); ?>
But weirdly it outputs this readmore link...
I seems to break up the URL of the image location and output it like this...
<img alt="" readmore-arrow.png"="" images="" my-theme-name"="" themes="" wp-content="" wp="" mywebsitename.co.uk="" src"http:="">
Can any one help me fix this?
Thanks
Working code...
<?php the_advanced_excerpt('length=120&use_words=0&no_custom=0&ellipsis=%26hellip;&rea‌​d_more=read more <img src="' . get_bloginfo('template_url') . '/images/readmore-arrow.png" alt="" />&add_link=1'); ?>
I'm not sure if this will fix it for you as i've not used that plugin, but you're missing an = for the src attribute. I've added it in here:
<?php the_advanced_excerpt('length=120&use_words=0&no_custom=0&ellipsis=%26hellip;&read_more=read more <img src="' . get_bloginfo('template_url') . '"/images/readmore-arrow.png" alt="" />&add_link=1'); ?>

PHP function return. Nested functions

I know there's Wordpress StackExchange, but that's more PHP related question.
I'm writing my own shortcode for Wordpress it looks like:
function myShortcode_shortcode() {
return 'something';
}
This shortcode displays simple string "something".
The problem is I want to display an image from template directory:
<img src="<?php bloginfo('template_directory') ?>/images/myImage.jpg" alt="" />
And I don't know how?
When I do:
return '<img src="'. bloginfo('template_directory') .'/images/myImage.jpg" alt="" />';
Script is echoing template directory instead of image.
Any ideas?
The problem is that the bloginfo() function is an output function (intended for templates). You need get_bloginfo() rather.
You probly need to place <img src="<?php bloginfo('template_directory') ?>/images/myImage.jpg" alt="" /> in his own variable like
function shortcode(){
$shortcode = "<img src='". bloginfo('template_directory') ."/images/myImage.jpg' alt="" />"
return $shortcode;
}
Hope this helps

Categories