View Full Version : Newbie question - stuck with PHP syntax

01-28-2012, 04:41 PM
Hi all,

First, I should say that I don't know any PHP. I usually manage to do minor changes in my wordpress template though. But this time I am stuck, though I actually think it should be easy if I just know some PHP.

I have a related articles plugin, which uses the following template:

if (have_posts()) {
$output .= '<div class="singlesidebar">';
while (have_posts()) {
$output .= '<div class="related"><img src="" height="85" width="65" /><h2"><a href="'.get_permalink().'">'.get_the_title().'</a></h2><p>'.get_the_time('d.m.Y').'</p></div>';
$output .= '</div>';

As you can see the image source is empty. I added the image and couldn't figure out how to add the url. The theme code that gets the image url looks like this:

<img src="<?php $thumb = get_post_meta($post->ID, "Thumbnail", true); echo $thumb; ?>" height="85" width="65" class="a-left-img" />

My only problem is that I don't know the PHP syntax. I have tried adding the thumbnail code to the related posts plugin, then tried adding only $thumb, tried adding echo $thumb and several other possibilities I could think of. Obviously nothing worked, all I got was T_STRING errors.

This might sound like a stupid question to you, but you would really help me. I am not a coder but I have to figure this out.

01-28-2012, 05:31 PM
You just want to take how the data in the second block creates the src attribute, and put that into the first block right? Just this:

$output .= '<div class="related"><img src="' . get_post_meta($post->ID, 'Thumbnail', true) . '" height="85" width="65" /><h2"><a href="'.get_permalink().'">'.get_the_title().'</a></h2><p>'.get_the_time('d.m.Y').'</p></div>';

Is what you want. The first block of code is a string assignment to a variable, so you are already in <?php. The second block is in HTML mode so whenever you need PHP to execute you need to add <?php ...?> to it, so this combined with (likely) a lack of ' around the string is probably what is triggering your T_STRING error.

01-28-2012, 05:47 PM
Thank you for the answer. I have tried your code and it doesn't give an error, but no image url either.

Maybe it is a wordpress thing after all?

01-28-2012, 05:52 PM
Don't know.
Check the HTML source to see if the img is provided with the img src. If it is, its a path issue. If its empty, the function isn't returning a result.

01-28-2012, 06:18 PM
I have checked the HTML, src is empty. I don't understand why the function returns a result at one place and not in the other.

01-28-2012, 06:23 PM
This assumes you have all variables available and functions to call.
Open the error reporting up to see if there is an error in it:

ini_set('display_errors', 1);

The only error I can see popping up that would effect the output and isn't fatal is that $post isn't a defined object and cannot access the property of id. If the function doesn't exist it would issue a fatal error.

01-28-2012, 06:57 PM
The error is:

Notice: Trying to get property of non-object in /www/htdocs/w0081759/bestseller/wp-content/plugins/yet-another-related-posts-plugin/template-widget.php on line 8

Line 8 is this:

$output .= '<div class="article2"><div class="a-left"><img src="' . get_post_meta($post->ID, 'Thumbnail', true) . '" height="85" width="65" class="a-left-img" /></div><div class="a-right"><h2 class="sisih2"><a href="'.get_permalink().'">'.get_the_title().'</a></h2><p class="sisidate">'.get_the_time('d.m.Y').'</p></div><div class="clear"></div></div>';

01-28-2012, 07:01 PM
And there is your answer. $post is not an object, so you need to figure out where this is supposed to come from.

01-28-2012, 10:33 PM
ok, thanks for your help.

01-29-2012, 05:45 PM
ok, thanks for your help.

Your welcome. Look on the page where you are pulling the second block of your original post from. Somewhere on there or one of its includes there will be an assignment to $post.