This seems like a really simple issue, but I'm not having any luck solving it and not entirely sure what to search. I am trying to get $logo to be used by the funciton, so that the image uses the variable defined on line 1:
$logo = 'http://dev.batman.com/logo.png';
function signup_email_body( $logo ){
$body = '<img src="'.$logo.'" />';
}
The funciton is then triggered on signup and the src attribute is missing.
(n.b. the above is simplified code to save masses of HTML)
The value of $body is never returned from your function so it essentially goes away once the function call is finished executing. You need to return that value from your function in order to actually use it:
$logo = 'http://dev.batman.com/logo.png';
function signup_email_body( $logo ){
return '<img src="'.$logo.'" />';
}
$body = signup_email_body( $logo ); // <-- now $body has that string value
Using sprintf() is a neat idea in these cases.
$logo = 'http://dev.batman.com/logo.png';
function signup_email_body($logo)
{
return sprintf('<img src="%s" />', $logo);
}
echo signup_email_body($logo);
Related
Ok, so I have literally never been so confused. As you can see I have pretty much the same function twice here (I know that may seem stupid but it is just easier for me to read for my page when it's like this - but that isn't the point of this)
The first one goes to the link it's given (http://www.blade-edge.com/images/KSA/Flights/craft.asp?db=dunai) then follows the path to get the img src of http://i.imgur.com/8t5rwWh.png
But the second function doesn't get the src of the image it's pointing to (which would be http://i.imgur.com/jWWUEqt.png) but instead gets the src for a completely different image on the page http://www.blade-edge.com/images/KSA/Flights/prev.png.
I am sure this is a really stupid mistake that I have just overlooked but I can't work it out.
Anyone?
function getImage(){
$page = file_get_html(getPageURL());
$element = $page->find("html/body/div/div[1]/center/table/tbody/tr[1]/td/table/tbody/tr/td[1]/div/div/img");
$imgLink = $element[0]->getAttribute("src");
echo "<img id='shipImage' src='".$imgLink."'></img>";
}
function getMap(){
$page = file_get_html(getPageURL());
$element = $page->find("/html/body/div/div[1]/center/table/tbody/tr[2]/td/center/img");
$imgLink = $element[0]->getAttribute("src");
echo "<img id='shipMap' src='".$imgLink."'></img>";
}
The following works for me:
function getImage($imageType){
$page = file_get_html(getPageURL());
$element = $page->find("/html/body/div/div[1]/center/table/tbody", 0)->children($imageType)->find("img");
$imgLink = $element[0]->getAttribute("src");
return $imgLink;
}
echo "<img id='shipImage' src='" . getImage(0) . "'></img>"; // Spacecraft image
echo "<img id='shipMap' src='" . getImage(1) . "'></img>"; // Map image
I won't try to guess the reason behind the problem, as I do not know the innards of the library.
I have searched this problem for a while now, maybe it is simple or maybe not. I could not figure out how to get this to work.
My goal outcome would be a hyperlink related to the post meta with some styling like so.
Check out the r_title here!
The code I have is:
<?php
$rtitle1 = get_post_meta($post->ID, 'r_title', true);
$rlink1 = get_post_meta($post->ID, 'href_link', true);
function testfunction() {
$output .= '<a href=\"'$rlink1'\" style=\"color: #e67300\" rel=\"nofollow\">';
$output .= ' Check out the '$rtitle1' here!</a>';
return $output;
}
add_shortcode('shortcode', 'testfunction');
?>
There are several problems with your code.
The first problem is with string concatenation. When you want to glue strings together you need to use the concatenation operator (the dot: .):
$end = 'a string';
$start = 'This is ';
$string = $start.$end;
If you just juxtapose variables and strings (or any other scalar types) then you will get errors:
$end = 'a string';
$string = "This is "$end; // Error!
The second problem is that you are using two variables ($rtitle1 and $rlink1) that are in the global scope. If you want to use global variables inside a function then you need to declare them as global inside the function:
$globalVar = 'test';
function test() {
global $globalVar;
echo $globalVar;
}
The third problem is that you forgot the ending closing parenthesis, ), for the get_post_meta() function:
$rtitle1 = get_post_meta($post->ID, 'r_title', true;
$rlink1 = get_post_meta($post->ID, 'href_link', true;
They should be like this:
$rtitle1 = get_post_meta($post->ID, 'r_title', true);
$rlink1 = get_post_meta($post->ID, 'href_link', true);
Before you think about asking for help you should look at the error messages that you get. If you have not seen the error message before then Google it. The best way to learn something is to find the solution on your own. Asking questions is for when you have tried finding a solution but you cannot find it.
I have a class with a method called paint() so i call $object->paint(); and it returns the html code to display the object the way i like it.
In a function, i have an array of those objects, so i do something like this:
$code = '<p class="wrapper">';
foreach( $object_arr as $object ){
$code .= $object->paint();
}
$code .='</p>';
echo $code;
but the result iḿ getting is this:
<p class="wrapper"></p>
<figure id="f1">Figure 1</figure>
<figure id="f2">Figure 2</figure>
...
<figure id="fn">Figure n</figure>
The function paint() returns the code to paint the object, i was expecting to see:
<p class="wrapper">
<figure id="f1">Figure 1</figure>
<figure id="f2">Figure 2</figure>
...
<figure id="fn">Figure n</figure>
</p>
What am i doing it wrong?
The result you're looking at might be corrected by your browser. p does not accept figure as a child, so the browser might correct it by closing your first <p class="wrapper">.
Some more code would be helpful, but this might put you on the right track.
What is $object_arr an array of. Can you post the example of the object in the object array?
With my example you would have to pass in the index as well to get the id="fnn".
If you post some more code I can clarify my answer.
<?php
function paint($str){
return '<figure id="fn">'.$str.'</figure>';
}
$object_arr =Array('happy','beans');
$code = '<p class="wrapper">';
foreach( $object_arr as $object ){
$code .= paint($object);
}
$code .='</p>';
echo $code;
If you're using single quotes, then put a tab before the method call:
$code .= ' ' . $object->paint();
But this would be easier:
$code .= "\t" . $object->paint();
I have a php function that generates HTML code like below
function j_uf_SomeFunction($some_var) {
?><div class="db_photo">
<img alt="<?php echo some_php_function ?>" src="<?php echo $some_var; ?>" />
</div><?php
}
Of course, its much more advanced and add all sorts of user options.
In most case I place this function inline, as opposed to have to append it to a string. However, I've come to the first occurrence (probably not the last occurrence) where I need to store the rendered HTML in a string and not have it sent straight off to the parser for building the page.
I need to cut the function off and tell it to take the html generated and store it in a string, and not send it off to the page, only on certain situations.
function j_uf_SomeFunction($some_var) {
ob_start();
?><div class="db_photo">
<img alt="<?php echo some_php_function ?>" src="<?php echo $some_var; ?>" />
</div><?php
return ob_get_clean();//suggestion by GWW
}
ob_start() is starting buffer receive
ob_get_clean() cleans current buffer and returns its value.
More info on http://php.net/manual/en/function.ob-start.php
ob * output buffering
It sounds like output buffers are one possible solution to your problem.
You use an output buffer like so:
ob_start();
j_uf_SomeFunction($someVar);
$buffer = ob_get_contents();
ob_end_clean();
The $buffer variable now contains anything printed out by the function.
It's important to always close output buffers with ob_end_clean or ob_end_flush. You can read more here: http://php.net/manual/en/book.outcontrol.php
Regards,
Chris
I don't I have a template system to parse this functions value into... its not your standard function call.
sure you do... its jsut contained within the function :-)
using translation:
function j_uf_SomeFunction($some_var) {
$html = "<div class="db_photo"><img alt="%some_function_result%" src="%some_var%" /></div>";
$tokens = array(
'%some_var%' => $some_var,
'%some_function_call_result%' => some_function_call()
);
return strtr($html, $tokens); // or echo
}
using string manipulation:
function j_uf_SomeFunction($some_var) {
$html = '<div class="db_photo"><img alt="%s" src="%s" /></div>';
return sprintf($html, some_function_call(), $some_var); //or echo
}
if some_function_call actually outputs html directly with its own echo then jsut use a buffer:
function j_uf_SomeFunction($some_var) {
ob_start();
some_function_call();
$somefunc = ob_get_clean();
$html = '<div class="db_photo"><img alt="%s" src="%s" /></div>';
return sprintf($html, $somefunc, $some_var); //or echo
}
Im looking to create a condition in wordpress loop. if no image then image box (.thumbHome{display:none})
this is in my function.php
function getThumbImages($postId) {
$iPostID = get_the_ID();
$arrImages =& get_children('post_type=attachment&post_mime_type=image&post_parent=' . $iPostID );
if($arrImages) {
$arrKeys = array_keys($arrImages);
$iNum = $arrKeys[0];
$sThumbUrl = wp_get_attachment_thumb_url($iNum, $something);
$sImgString = '<img src="' . $sThumbUrl . '" alt="thumb Image" title="thumb Image" />';
echo $sImgString;}
else {
echo '<script language="javascript">noImage()</script>';
}
}
And my javascript:
window.onload = noImage();
function noImage(){
document.getElementByClassName('.thumbHome').css.display = 'none';
}
I tried:
window.onload = noImage();
function noImage(){
$('.thumbHome').addClass('hide');
}
RESULT: class hide added to all loop
I cant figure it another way, since im still new in coding.
thx
Well first of all, you don't want to call these functions on window.onload. That's going to immediately set all class instances of .thumbHome to hidden without any conditions.
Here's a very easy way to fix this issue. There are probably more intricate ways, but this works well.
In your main loop, add an unique id to each .thumbHome div based on the image id. So like:
echo '<div class="thumbHome" id="thumb-' . $iNum . '"> ... </div>';
// or you could you use the post ID, doesn't matter, as long as you are consistent
Then your else conditional (for whether there's a thumbnail) could be changed to:
else {
echo '<script type="text/javascript">noImage("#thumb-' . $iNum . '")</script>';
}
and your js function could be:
function noImage(var){
$(var).hide();
}
This is not necessary the best way to do this, it's just the best way with the situtation you find yourself in now.