• Resolved kapa0

    (@kapa0)


    Hello. I’m trying to create a template for a certain page so that it will display, aside from the page’s content, a box of the latest posts from a chosen category.

    Here’s the code from the page template I’ve come up with:

    <?php get_header(); ?>
    
    <div id="content"><a name="content"></a>
    <?php if (have_posts()) : ?>
    
    <?php while (have_posts()) : the_post(); ?>
    <div class="single" id="post-<?php the_ID(); ?>">
    <div class="title">
    
    <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
    
    </div>
    
    <div class="cover">
    <div class="entry">
    					<?php the_content('Read the rest of this entry &raquo;'); ?>
    
    </div>
    
    </div>
    
    </div>
    		<?php endwhile; endif; ?>
    
    </div>
    <div id="newsbox">
    <h3>What's New</h3>
    <?php query_posts('cat=18'); ?>
    </div>
    
    <?php get_footer(); ?>

    The What’s New displays properly and in the proper place on the page, but there’s nothing afterward. I’ve gone through the codex, and I’ve got the rudiments of how tags work, but I’m still pretty new to WordPress. I’m guessing the solution has to do with the loop, but I don’t know how to fix it.

Viewing 2 replies - 1 through 2 (of 2 total)
  • In my opinion, the best way for you to do so will be to use Multiple Loops.
    you have several way of doing so.

    query_posts() perform a new query, build a new posts array, and reset the loop counter. but then you have to loop again in order for it to show.

    <?php query_posts('category_name=mycategory'); ?>
      <?php while (have_posts()) : the_post(); ?>
        <!-- your template stuff here -->
      <?php endwhile;?>

    you might need to reset the posts befor using

    <?php rewind_posts(); ?>

    (depending on other template functions.

    Another way is to use a new custom query :

    <?php $my_query = new WP_Query('category_name=MyCategory'); ?>
    
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
      <!-- your template stuff here -->
    '<?php endwhile; ?>

    so the end page can look something like this :

    <?php $my_query = new WP_Query('category_name=MyCategory');
    while ($my_query->have_posts()) : $my_query->the_post();
    $do_not_duplicate = $post->ID;?>
    <!-- your template stuff here -->
    <?php endwhile; ?>
    
    <!-- Do whatever your template wants... -->
    
    <?php if (have_posts()) : while (have_posts()) : the_post();
    if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>
    <!-- your template stuff here -->
    <?php endwhile; endif; ?>'

    note that this will remove duplicated ID’s , so they will not show up both in the main loop, and in the secondary .. if you DO want them to show, you will need to remove the $do_not_duplicate array.

    Thread Starter kapa0

    (@kapa0)

    Thanks! 🙂

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘query_posts on a page’ is closed to new replies.