• Resolved Adam W. Warner

    (@awarner20)


    I am trying to pull in the latest post of a specific category into a page. I am using the exec-php plugin that allows me to insert php. I can’t seem to get my query correct because it’s pulling two of the same post and displaying them in my page. Can anyone tell me what’s wrong with my query?

    <?php query_posts('cat=3&posts_per_page=1&offset=1'); ?>
    <?php if (have_posts()) : ?>
       <?php while (have_posts()) : the_post(); ?>
         <div class="post">
          <h2 id="post-<?php the_ID(); ?>">
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
    <?php the_title(); ?></a></h2>
    </div>
    <?php endwhile; ?>
    <?php else : ?>
      <h2 class="center">Not Found</h2>
    <p class="center"><?php _e("Sorry, but you are looking for something that isn't here."); ?></p>
     <?php endif; ?>

    Any advice would be greatly appreciated.

Viewing 2 replies - 1 through 2 (of 2 total)
  • You can leave the standard wordpress loop as it is and use a new query so as to not conflict with the standard loop (and use as many times as needed on a page/post):

    <?php $my_query = new WP_Query('category_name=mycategory&showposts=1'); ?><?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <div class="post">
    <h2 id="post-<?php the_ID(); ?>">
    <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
    <?php the_title(); ?></a></h2></div>
    <?php endwhile; ?>
    Thread Starter Adam W. Warner

    (@awarner20)

    Hi songdogtech,

    Thank you very much for the guidance. This is working perfectly, thank you!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Using query_posts to get the lastest post title inside a page.’ is closed to new replies.