Help With Wordpress Recent Posts

Discussion in 'WordPress' started by sarahturned, Jun 1, 2009.

  1. #1
    How do I add recent posts with thumbnail images to one of my two sidebars on my wordpress blog?
     
    sarahturned, Jun 1, 2009 IP
  2. susan8051

    susan8051 Peon

    Messages:
    1,358
    Likes Received:
    49
    Best Answers:
    0
    Trophy Points:
    0
    #2
    susan8051, Jun 1, 2009 IP
  3. BANAGO

    BANAGO Active Member

    Messages:
    456
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #3
    You need a custom field and a custom WP_query loop. Do you know how to do that?
     
    BANAGO, Jun 1, 2009 IP
  4. sarahturned

    sarahturned Peon

    Messages:
    268
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I'm comfortable with creating thumbnails and the custom fields, I've read up on that, I'm just not sure what code I need to add to the sidebar.php and where in the sidebar.php to add it so that I can achieve this effect.
     
    sarahturned, Jun 1, 2009 IP
  5. susan8051

    susan8051 Peon

    Messages:
    1,358
    Likes Received:
    49
    Best Answers:
    0
    Trophy Points:
    0
    #5
    in the side bar make
    <?php
    query_posts('showposts=5');
    while (have_posts()) : the_post(); 
    ?>
      // image with permalink
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><img src="<?php echo bloginfo('template_url'); ?>/scripts/timthumb.php?src=<?php echo get_option('home'); ?>/<?php
    $values = get_post_custom_values("Image"); echo $values[0]; ?>&amp;w=<?php echo $width; ?>&amp;h=<?php echo $height; ?>&amp;zc=1&amp;q=100"
    alt="<?php the_title(); ?>" class="left" width="<?php echo $width; ?>px" height="<?php echo $height; ?>px"  /></a>
    <?php
    endwhile;
    ?>
    Code (markup):
    this is how it works if the custom field name is Image
    width and height are the parameters for generating the thumbnail which shud be assigned values
    as i said you can also user the first(or any image) in your blogpost to generate the thumbnail..
     
    susan8051, Jun 2, 2009 IP
  6. BANAGO

    BANAGO Active Member

    Messages:
    456
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #6
    Here is the code you need. Edit category name and number of posts as you wish.


    <?php $my_query = new WP_Query('category_name=sidebarposts&showposts=5'); 
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
    
    <div class="featured_post" id="post-<?php the_ID(); ?>">
    
    <img src="<?php echo get_post_meta($post->ID, 'Thumb', true) ?>" alt="" class="thumb" />
    
    <h3 class="title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>  </h3>
    
    <?php the_excerpt() ?> 
    
    </div>  
    
    <?php endwhile; ?>
    PHP:
     
    BANAGO, Jun 2, 2009 IP
  7. BANAGO

    BANAGO Active Member

    Messages:
    456
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #7
    query_posts is not advices to be used for extra queries. Rather it is used to edit the main query loop. For extra queries you need to use WP_Query.
     
    BANAGO, Jun 2, 2009 IP
  8. sarahturned

    sarahturned Peon

    Messages:
    268
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    This code worked really well, Thanks.

    The code showed the last 5 posts in a specific category. Is there a way to show the last 5 most recent posts on the blog, regardless of what category they are in?
     
    sarahturned, Jun 3, 2009 IP
  9. susan8051

    susan8051 Peon

    Messages:
    1,358
    Likes Received:
    49
    Best Answers:
    0
    Trophy Points:
    0
    #9
    $my_query = new WP_Query('category_name=sidebarposts&showposts=5');

    change this to

    $my_query = new WP_Query('showposts=5');
     
    susan8051, Jun 3, 2009 IP
  10. sarahturned

    sarahturned Peon

    Messages:
    268
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    This is great stuff...thanks.

    Is there a similar query to show 5 related posts?
     
    sarahturned, Jun 3, 2009 IP