Support » Themes and Templates » How to query and display one post in the sidebar

  • Hello everyone,

    I need to show one post in a sidebar, this have to be define with post id.
    I also need to be able to add my customization so it blends with the site. Can anyone help me? I already checked the WordPress query_post, but because I still new to WP it is kind of hard to figure it out.


Viewing 3 replies - 1 through 3 (of 3 total)
  • I don’t know how correct this is, I am not a php programmer. However, this is what I have done in the sidebar, you’ll just have to modify it from category name / show 1 post, to using post id (you can find that info in the codex).

    <?php query_posts('category_name=news&showposts=1'); ?>
      <?php while (have_posts()) : the_post(); ?>
       <h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title() ?></a></h2>
     <?php the_post_thumbnail(array(100,100), array('class' => 'alignleft post_thumbnail'));?>
                  <?php the_content() ?>
           <div class="clear"></div>
      <?php endwhile;?>

    Hi wpit,
    You can try :

    //First we select the post id that you want to use ex: 5
    query_posts( 'p=5' );
    //then the loop stats here:
    if ( have_posts() ) : while ( have_posts() ) : the_post();
    //and ends here :
    endwhile; else: ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>


    Thank you guys,
    Both worked, jnhghy was more specific to what I was looking for. After testing I also found out that you need <?php wp_reset_query(); ?> at the of the<?php endif; ?> if not single.php will always display the one post I am querying.


Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to query and display one post in the sidebar’ is closed to new replies.