Forums

Why Do i have to include a 2nd query_post? (2 posts)

  1. arcab4
    Member
    Posted 4 years ago #

    I am trying to show posts (up to 3 - hence the $count) that are in two categories only. the code i used is:

    <? $count = 0 ; ?>
    <?php query_posts(array('category__and'=>array(3,46))); ?>
    <?php while (have_posts()) : the_post(); ?>
    <?if ($count<3) {?>
    <span class="listings"><a href="<?php the_permalink(); ?>" title="Permanent link to <?php the_title(); ?>"><?php the_title('','','&raquo;'); ?></a></span> <?php the_excerpt('Read the rest of this entry &raquo;') ?><a href="<?php the_permalink(); ?>" title="Permanent link to <?php the_title(); ?>">Click Here To Continue</a>&nbsp;<br>&nbsp;<br>
    <? } ?>
    <? $count ++;?>
    <?php endwhile;?>
    <?php query_posts('cat=12'); ?>

    for some reason, i have to add in the last line:

    <?php query_posts('cat=12'); ?> - which queries a non-existent category

    if i don't do that, it shows the posts in it's entirety. why would i need that 2nd query post?

    any help would be appreciated it.

    Thanks,

  2. Otto
    Tech Ninja
    Posted 4 years ago #

    Well, I suspect that you're using this code somewhere else and another Loop is occurring afterwards, in code you have not shown us. This code is being called from somewhere and then going back to another Loop. Examine the flow of the page, see where that other Loop is.

    But, I also know that you're doing things the hard way. This for example:

    <?php query_posts(array('category__and'=>array(3,46))); ?>
    <?php while (have_posts()) : the_post(); ?>
    <?if ($count<3) {?>
    ...
    <? } ?>
    <? $count ++;?>
    <?php endwhile;?>

    Looks like you want to display three posts. So you have a loop with an incremental counter? Does that make any sense to you? Why not just get only 3 posts to begin with, and show those?

    <?php query_posts(array(
    'category__and' => '3,46',
    'showposts' => 3,
    ));
    while (have_posts()) : the_post(); ?>
    ...
    <?php endwhile; ?>

    Simpler, eh?

Topic Closed

This topic has been closed to new replies.

About this Topic