WordPress.org

Ready to get started?Download WordPress

Forums

Multiple Loops- what am I doing wrong? (12 posts)

  1. jessn
    Member
    Posted 4 years ago #

    Hi everyone! What I'm trying to do is create a page template that will do two things:

    1. Look at the content for that page in the admin and display it at the top
    2. Loop through the newest posts of a given category and display them below.

    For some reason I cannot get the page content to display. At the top instead it's showing newest posts from a different category, and then the newest posts from the correct category below.

    I guess my question is: How can you have two loops on the same page, one to look at and display the page content, and another to loop through some posts?

    I can display my code if needed! Thank you!

  2. Mark / t31os
    Moderator
    Posted 4 years ago #

    Please use a pastebin and post your code.

  3. jessn
    Member
    Posted 4 years ago #

  4. You need a new query to run multiple loops on the same page; this is what I use to display the title and permalink of the latest post in "mycategory":

    <?php $my_query = new WP_Query('category_name=mycategory&showposts=1'); ?><?php while ($my_query->have_posts()) : $my_query->the_post(); ?><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a><?php endwhile; ?>

    Change the_title to the_content or the-excerpt, as needed. If you use this code in a post or page from the WP editor, you also need to use a plugin that allows you to execute php, like Exec-PHP « WordPress Plugins. Also see: Function Reference/WP Query « WordPress Codex

  5. jessn
    Member
    Posted 4 years ago #

    Thanks songdogtech. Yeah originally I had the second loop set up that way and that worked great too. However I need to be able to paginate the posts when there are more than ten which I why I set it up the second way

    ...
    $wp_query->query('showposts=10&category_name=taste-gym'.'&paged='.$paged);
    ...

    My problem is with the first loop and it not displaying the page content. For example if I call this page "About" and assign the about page template to it, I want all wording that I fill in in the About page to show at the top, with recent posts from a given category shown below.

  6. t310s_ and I had a timing collision on this :)

    Don't know off hand. I'll have to think about this for a bit and look at pastebin. t310s_ probably knows quicker.....

  7. Mark / t31os
    Moderator
    Posted 4 years ago #

    You need to have this at the top.

    <?php
    /*
    Template Name: NAMEFORTEMPLATE
    */
    ?>

    So i can't work out how you've attached it to a page, since it won't show up to select until the above lines are present.

    Secondly, contrary to belief, you don't need a new query, rewind_posts allows you basically roll the query back and re-set any parameters you want.

    I've tested your code (with the above added) and it works no problem for me.

    No collision songdogtech, i don't mind who helps, i'll give input if necessary, feel free to participate as much as you like.... ;)

  8. jessn
    Member
    Posted 4 years ago #

    Thanks! Yes that's at the top of the page, just didn't think it was necessary to paste it in the pastebin.

    Aha! After you mentioned that it was working for you I tried removing the sidebar and suddenly it's working the way it should. So there's something in my sidebar interfering. I'll try to track it down. Thanks for your help!

  9. Mark / t31os
    Moderator
    Posted 4 years ago #

    What's in your sidebar?...

    Proberly just need to run a reset after any custom queries you have in there.

    wp_reset_query();

    Or maybe even before, depending on whether your sidebar is included before or after the page content.

  10. I didn't know that about rewind_posts and wp_reset_query....

  11. Mark / t31os
    Moderator
    Posted 4 years ago #

    A query is still a query at the end of the day, but if you're loading any page on a WordPress, as far as i can tell query_posts will always run. So my thinking is, if it's always running anyway, then use it where you can and only refer to a new query when absolutely necessary.

    I'm not totally sure on the differences between reset and rewind, but both help in re-using query posts (though usually and/or, not both at once).

    It may not even be a huge deal unless you're running a heavy traffic site, in which case every needless or un-necessary query matters, but for the most part (for people like me - with small sites), "whatever works" will suffice... :)

  12. groomedmonkey
    Member
    Posted 4 years ago #

    wp_reset_query();

    o m f g ... that solved EVERYTHING! I had a query in the sidebar listing posts from a tag in a custom taxonomy but running two queries kept screwing up my main content - so all post links went to post #1 data rather than the data for that link.

    That little line of code rocks! Thank you.

Topic Closed

This topic has been closed to new replies.

About this Topic