WordPress.org

Ready to get started?Download WordPress

Forums

How to query and display one post in the sidebar (4 posts)

  1. wpmhweb
    Member
    Posted 2 years ago #

    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.

    Thanks,

  2. AardvarkGirl
    Member
    Posted 2 years ago #

    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;?>
  3. jnhghy
    Member
    Posted 2 years ago #

    Hi wpit,
    You can try :

    <?php
    //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; ?>

    Regards

  4. wpmhweb
    Member
    Posted 2 years ago #

    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.

    Thanks,

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags