• I was reading up on the codex, and a few articles on multiple loops that was posted by infovere and ifelse- I may have to do something like this by using the loop several times in pages.

    I’ve used the following example:

    <?php $my_query = new WP_Query('category_name=film&showposts=1'); ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    // Do stuff.
    <?php endwhile; ?>

    The result just displays “do stuff” https://reviews.sekhu.net/. What I’m trying to do is call 1 post from each cat for each box. Take into account there is no other code apart from some content and css, nothing more – no loop etc. I would like to use this method, but in this instance use an excerpt with multiple loops? WOuld this work? If so how?

Viewing 15 replies - 1 through 15 (of 17 total)
  • You can specify multiple categories by their numeric category ID using 'cat=1+5+10+20', but I don’t know of way of using WP_Query to pull in only the last post from each category.

    Am I right in saying that wp_query is still largely undocumented. Am I also right in thinking that the variables do in fact include a reverse chronology ?

    Edit: Sorry. I meant query posts.

    I’m not sure what it has to do with Jinsan’s query, but yes query_posts is still largely undocumented, and it does allow for listing posts in either DESCending or ASCending order, though not just by post date.

    Thread Starter jinsan

    (@jinsan)

    so, in theory, I could pull 1 post which would be in DESC order and that would essentially display the recent post since it is going in DSEC order.

    Taking this into account, how would I display simply the excerpt in this situation with the above variables?

    The other problem is I the initial code for the posts breaks the theme: <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    I tried to insert that after <php? get_header (); > or something of that ilk in the index.php (which I’ve renamed home.php.

    Perhaps I misunderstood the original question.

    If you set up single iterations for each category, 'order=DESC' will certainly list only the lastest post, but this *is* the default (as is ordering by post date).

    Displaying an excerpt is no different than in the standard Loop:

    <?php $my_query = new WP_Query('category_name=film&showposts=1'); ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <?php the_excerpt(); ?>
    <?php endwhile; ?>

    Any tags used within The Loop should function in your custom one.

    “The other problem is …”

    Since it could be any number of problems, seeing how the template is laid out would be necessary to see where it may be breaking the layout.

    Thread Starter jinsan

    (@jinsan)

    Kaf – what would you need to see the layout? If you click view source on the site, that is all there is in the page. In terms of loop it is non existent – the start and end of the loop do not exist, and I’m not sure where to start here. Only the CSS and some html is there to:

    1) design the page layout
    2) insert images

    Nothing more. Let me know how to get the template to you and I will do so. If I can at least get some sort of loop in there, it would be a great start and then the excerpt code will probably work properly.

    Thread Starter jinsan

    (@jinsan)

    I tried to create a phps file without success – I thought it was a case of just sticking an s at the end of it, but even a google found nothing for me. So here’s the index.php/home.php in full. Hope osmeone can tell me where to place The Loop:

    <?php get_header(); ?>
    <div id="lmain">

    <div class ="lexcerpt medium box">
    <div class="excerptimage1"></div><div class="excerpttitle">Film</div>
    <?php $my_query = new WP_Query('category_name=film&showposts=2'); ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    // Do stuff.
    <?php endwhile; ?>
    </div><!-- Close box2-->

    <div class="rexcerpt medium box">
    <div class="excerptimage2"></div><div class="excerpttitle">Music</div>
    <?php c2c_get_recent_posts(1, "%post_excerpt%", "3"); ?>
    </div><!-- Close box3-->

    <div class="clearer">
    &nbsp;
    </div>

    <div class ="lexcerpt medium box">
    <div class="excerptimage3"></div><div class="excerpttitle">Book</div>
    <?php c2c_get_recent_posts(1, "%post_excerpt%", "5"); ?>
    </div><!-- Close box2-->

    <div class="rexcerpt medium box">
    <div class="excerptimage4"></div><div class="excerpttitle">Game</div>
    <?php c2c_get_recent_posts(1, "%post_excerpt%", "4"); ?>

    </div><!-- Close box3-->

    <div class="clearer">
    &nbsp;
    </div>

    <div id="posts">
    <div class="latestimage"></div><div class="latesttext">The Latest Reviews</div><br />
    This is where the links for the latest posts would go. A mixed group of links with the title latest entries. Or something like that. This is where the links for the latest posts would go. A mixed group of links with the title latest entries. Or something like that. This is where the links for the latest posts would go. A mixed group of links with the title latest entries.
    </div>

    <div class="clearer">
    &nbsp;
    </div>
    </div>

    <div class="clearer">
    &nbsp;
    </div>

    <div id="rcomment" class="box">
    <div class="speechimage"></div><div class="speechtext">Latest Comment</div><br />
    </div>

    <div id="kmain">

    <div id ="lcomment" class ="short box">
    <div class="speechimage"></div><div class="speechtext">Latest Comment</div><br />
    </div>

    <div id="rrcomment" class="box">
    <div class="speechimage"></div><div class="speechtext">Latest Comment</div><br />
    </div>
    <?php
    get_footer();
    ?>

    Thread Starter jinsan

    (@jinsan)

    Using the loop as is, I actually managed to get it running – so it does work with the design, the problem is there was a lot of madness.

    FIrstly there were about 8 posts – I guess this was because of reading>posts which spread vertically down the page without styling. This broke the design. So how would I make this loop sit in the background, but not actually display anything, letting instead the plugin do its excerpt.

    Do I need to copy and paste this loop once for each cat container? If I set the reading>posts to 1 it will only display one post, but I don’t want to to show anything but one post for each cat block. How woudl I go about this?

    Cheers

    There is currently only one option in admin. Number of posts to be displayed. At the moment therefore I think the best we can do is create cats and allocate one single post to each cat (the one we want displayed). That is how I pick my lead story. You seem to want something similar but in multiples. So each of your boxes contains a single post from a single cat. This is what I use:
    <?php
    $my_query = new WP_Query('cat=2&showposts=1');

    while ($my_query->have_posts()) : $my_query->the_post();
    ?>

    NB. That is independent of the loop

    Thread Starter jinsan

    (@jinsan)

    interesting root – so would I actually need the loop to be contained within the php file or can I add that query without the presence of the loop in any file, or does it need to exist somewhere for hte call to work? Having tried that code, it provided a php error for get footer – removing get footer noves the error to an innocent little div. so I guess would need some sort of loop and then to call that query?

    Sorry I seem to have missed out a bit. Here is the full monty. But it runs prior to the main loop even getting started.


    <?php
    $my_query = new WP_Query('cat=2&showposts=1');

    while ($my_query->have_posts()) : $my_query->the_post();
    ?>

    <h3 class="storytitle" id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
    <div class="meta"><?php _e("Filed under:"); ?> <?php the_category(',') ?> — <?php the_author() ?> @ <?php the_time() ?> <?php edit_post_link(__('Edit This')); ?></div>
    <img src="https://atthe404.com/wp-content/themes/aphrodite/images/bluehead.gif&quot; class="topimage" alt="blue head image" />

    <?php the_content('and there is more....'); ?>

    <?php endwhile; ?>

    Thread Starter jinsan

    (@jinsan)

    nice one;) ok it’s moving me in the right direction but something is a little odd in that cat2 which is film is displaying an old post that I have deleted rather than the current post -but the deleted post keeps returning.

    cat3 (blue) = music but displays the music cat without the permalink
    cat 4 (green) = displays excerpt for cat5 and permalink for cat4
    cat5 (yellow) = book displays excerpt for cat and permalink for cat4

    What’s going on with here? I copied the code and changed the cat2 to cat 3, 4, 5 respectively and this is what happened so I’m wondering if there is a conflict or a problem with something else?

    Cheers

    Not sure about this.It is something to do with the variable not resetting to zero content.

    How about adding this prior to the endwhile:
    new WP_Query = $my_query

    Thread Starter jinsan

    (@jinsan)

    you mean:

    <?php new WP_Query = $my_query->endwhile; ?> ? That gives me an unexpected = error.

    Or would it be

    <?php new WP_Query = $my_query; ?>
    <?phpendwhile; ?>

    ?

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘Using multiple loops’ is closed to new replies.