WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Recent post images in sidebar (7 posts)

  1. Massifsam
    Member
    Posted 2 years ago #

    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.

  2. esmi
    Forum Moderator
    Posted 2 years ago #

    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();
  3. Massifsam
    Member
    Posted 2 years ago #

    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.

  4. esmi
    Forum Moderator
    Posted 2 years ago #

    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();
  5. Massifsam
    Member
    Posted 2 years ago #

    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?

  6. esmi
    Forum Moderator
    Posted 2 years ago #

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

  7. Massifsam
    Member
    Posted 2 years ago #

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

    Thanks again esmi.

Topic Closed

This topic has been closed to new replies.

About this Topic