WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Recent post images in sidebar

[Resolved] Recent post images in sidebar

  • Hello All,

    I’m a bit of a beginner at this and could do with your combined genius.

    I’ve been searching for a while trying to find a way to list the 6 most recent posts in the sidebar of my site purely with an image, and without the use of a widget. I still want the image to link to the post and I’m trying to add the image through a Custom Field as I want the image to be a specific crop rather than a full thumbnail, which I’m happy to do manually.

    I’ve found a few bits of code but my coding isn’t that strong yet and they either list a single post, don’t link through to the post or have a lot of resizing code built in which after a lot of playing around I just can’t seem to get working.

    Any help would be greatly appreciated.

Viewing 6 replies - 1 through 6 (of 6 total)
  • esmi

    @esmi

    Forum Moderator

    Try something like:

    $args = array(
    	'posts_per_page' => 6,
    	'meta_key' => 'image'
    );
    $the_query = new WP_Query( $args );
    echo '<ul class="recent-posts">';
    while ( $the_query->have_posts() ) : $the_query->the_post();
    echo '<li><a href="' . get_permalink() . '"><img src="' .  get_post_meta($post->ID, 'image', true) . '" width="xx" height="yy" alt="' . get_the_title() . '" />' . get_the_title() . '</a></li>';
    endwhile;
    echo '</ul>';
    wp_reset_postdata();

    Thanks a load Esmi.

    Am I right in thinking I should be updating ‘image’ to my Custom Field key? I’m getting a “Parse error: syntax error, unexpected T_STRING, expecting ‘,’ or ‘;’ on the meta_key line.

    esmi

    @esmi

    Forum Moderator

    Am I right in thinking I should be updating ‘image’ to my Custom Field key?

    Yes. I’ve also assumed that your custom field’s value contains the full (absolute) url for the image.

    I can’t see any obvious syntax error around the meta-Key in $args but I did notice something I should have removed from the code before I posted it. Try this:

    $args = array(
    	'posts_per_page' => 6,
    	'meta_key' => 'image'
    );
    $the_query = new WP_Query( $args );
    echo '<ul class="recent-posts">';
    while ( $the_query->have_posts() ) : $the_query->the_post();
    echo '<li><a href="' . get_permalink() . '"><img src="' .  get_post_meta($post->ID, 'image', true) . '" width="xx" height="yy" alt="' . get_the_title() . '" /></a></li>';
    endwhile;
    echo '</ul>';
    wp_reset_postdata();

    That’s sorted it, thanks so much for your help esmi.

    If I wanted the image to change to another image on hover (ie another custom field) is that possible, or not because we’ve got everything working under the original ‘image’ meta?

    esmi

    @esmi

    Forum Moderator

    That would be a lot more difficult and possibly best addressed using jQuery.

    Heh heh, thought so, but wanted to check it wasn’t something simple that I might’ve missed.

    Thanks again esmi.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘[Resolved] Recent post images in sidebar’ is closed to new replies.
Skip to toolbar