Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Newbie question - stuck with PHP syntax

    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:

    Code:
    <?php
    if (have_posts()) {
    	$output .= '<div class="singlesidebar">';
    	while (have_posts()) {
    		the_post();
    		$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:

    Code:
    <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.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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:
    PHP Code:
            $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.

  • #3
    New Coder
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.

  • #5
    New Coder
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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:
    PHP Code:
    <?php
    ini_set
    ('display_errors'1);
    error_reporting(E_ALL);
    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.

  • #7
    New Coder
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:
    Code:
    $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>';

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    And there is your answer. $post is not an object, so you need to figure out where this is supposed to come from.

  • #9
    New Coder
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok, thanks for your help.

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Quote Originally Posted by Aedan View Post
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •