WordPress.org

Ready to get started?Download WordPress

Forums

Pages - using the loop and query_posts (4 posts)

  1. ericmcgregor
    Member
    Posted 7 years ago #

    Hi.

    I have created a page called 'articles' (id=2).

    On this page I would like to have the page title and body, I would also like to query all posts from a particular catagory, AND I need to run list_pages() and have it list the 'articles' page as being current. whew!

    If I use query_posts() to try and pull in all posts from my category, it breaks list_pages() knowing that this is the current page and applying the current_page_item class the the li tag.

    I have done some digging and it turns out this is because apparently query_posts() resets the query object? (whatever THAT means) ;) Essentially, I believe that query_posts is doing something to list_pages preventing it from knowing that the page its on is the current one.

    Any help on how to pull specifc post catagories into a page would really help. Thanks!

  2. ericmcgregor
    Member
    Posted 7 years ago #

    Ok, I figured it out!

    On this page: http://codex.wordpress.org/The_Loop
    Scroll down, you'll see this:

    <?php $my_query = new WP_Query('category_name=special_cat&showposts=10'); ?>

    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <!-- Do special_cat stuff... -->
    <?php endwhile; ?>

    <?php rewind_posts(); ?>

    What you want to do is build your own query, then reweind posts to reset the query object so that list_pages doesn't break.

  3. davidchait
    Member
    Posted 7 years ago #

    actually, another method is to simply 'store' into variables the bits about the initial query (such as the categories) before starting your custom query. Then refer to that anywhere later on the page.

    -d

  4. ericmcgregor
    Member
    Posted 7 years ago #

    Thanks david. I'm not sure what that means though. I'm not a programmer, and so my level of understanding is very basic. Maybe you could provide an example?

Topic Closed

This topic has been closed to new replies.

About this Topic