• Resolved VicePrez

    (@viceprez)


    Im having trouble with “multiple loops”. I read the codex on “the loop”, along with some others. Im really new to this, ive never built a website before, and im learning fairly quickly.

    In essence, what i’m trying to do is the following:

    Category X: List one post

    Category Y: List one post

    List all other posts, minus the two posts.

    The idea behind this, is to list Category X and Y side by side, at the top of my page, then continue and have all my others posts (from all categories) listed below them. (the styling is complete, the PHP coding is what im having trouble with.

    This is what ive got so far for category X (featured)

    <?php $my_query = new WP_Query(‘category_name=featured&showposts=1’);
    while($my_query->have_posts()) :
    $my_query->the_post();
    $do_not_duplicate = $post->ID; ?>

    and this is for Category X (news)

    <?php $my_query = new WP_Query(‘category_name=news&showposts=1’);
    while($my_query->have_posts()) :
    $my_query->the_post();
    $do_not_duplicate = $post->ID; ?>

    Then i used this to post all my posts except for the other two, except it removes the post from category Y (news), and doesn’t remove the one from category X (featured)

    <?php query_posts(array(‘post__not_in’=>array($do_not_duplicate)));
    if (have_posts()) : while (have_posts()) : the_post();?>

    I added the “array” in case i wanted to show more than one post and have both removed (as far as i understood from the one of the codex pages, nevertheless it works).

    Ive spent the past 4-5 hrs digging and trying different types of things to get it to work. I just want those two posts removed from all my other posts, from all categories (including category X and Y)

    Thanks in advance guys.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    try it with this for both $do_not_duplicate = $post->ID;.

    $do_not_duplicate[] = $post->ID;

    Thread Starter VicePrez

    (@viceprez)

    im sorry keesie, but that doesnt seem to work, i did notice something while trying that. Ill try to illustrate it.

    i pasted the following for Category X:

    $do_not_duplicate = $post->ID;

    and the following for Category Y:

    $do_not_duplicate[] = $post->ID

    and i kept the following for all my posts

    <?php query_posts(array(‘post__not_in’=>array($do_not_duplicate)));
    if (have_posts()) : while (have_posts()) : the_post();?>

    well, what i noticed is that when i did that, Category Y’s post shows up in *all my posts* and Category X’s post disappears, when it was the other way round before you gave me the new coding. I know now that at least the coding (for all my posts) reads whats being passed down from Category X.

    any other suggestions? i think your on the right track.
    thanks again.

    Moderator keesiemeijer

    (@keesiemeijer)

    Do this for the first to loops $do_not_duplicate[] = $post->ID;
    and this for the third.

    <?php query_posts(array('post__not_in'=> $do_not_duplicate));
    if (have_posts()) : while (have_posts()) : the_post();?>

    Thread Starter VicePrez

    (@viceprez)

    keesie that worked so elegantly. thanks alot.

    if i dont mind asking, how does this variation differ from the original ones? whats so special about them:

    $do_not_duplicate[] = $post->ID

    vs.

    $do_not_duplicate = $post->ID

    ..?

    by the way, thanks again, i can focus on other things now. your a life saver.

    Moderator keesiemeijer

    (@keesiemeijer)

    $do_not_duplicate is a variable and $do_not_duplicate[] becomes a numerically indexed array (so it can hold multiple values) when you pass a value to it.

    Thread Starter VicePrez

    (@viceprez)

    oh, alright that makes alot of sense now.

    by the way, thank you for your time. i really appreciate it.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Multiple loops, 2 Categories, All Posts’ is closed to new replies.