WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] 2.1+ Won't Allow Multiple Pages With the_loop? (6 posts)

  1. jimmyjohn
    Member
    Posted 7 years ago #

    I've been working on solutions to a previously-described problem, and come upon a half-solution that makes things potentially quite a lot worse for those of us with 2.1+.

    My problem was that I couldn't get a custom page ("Schedule") with a custom template ("schedule.php") to load The Loop. I discovered under Options --> Reading that if I changed a setting I'd previously established, for the Front Page to display a static page, that if I set the "Posts Page" to "Schedule", all of a sudden my Schedule Page was using The Loop -- though it wasn't using it with the conditions set by schedule.php, suggesting that it was now ignoring its default template and simply using index.php.

    So, temporarily, I switched the "Front Page Displays" setting to "Your Latest Posts" so that there's no specified "Posts Page". Well, now the Schedule Page and schedule.php once again won't load The Loop, only content I create in the Schedule Page (should I choose to do so).

    It appears, given this and other testing of code that I've done on this problem, that 2.1+ limits the number of Pages that can load to The Loop to 1.

    Can this really be so? Or are there some new ways of loading The Loop for a custom Page and Template in 2.1+?

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    Err... Huh?

    All pages use The Loop. The index, the Page Template, the Single pages, everything. The difference is in what posts get loaded.

    If you load a Page, then the content of that page is what gets pulled from the database. The Loop then displays it.

    If you load a Single Post, then the content of that post is what gets pulled from the database. The Loop then displays it.

    If you load the home page, then several posts is what gets pulled from the database. The Loop then displays them.

    And so on.

    Now, what the static front page "Posts Page" option does is to change what happens when that Page is loaded. Instead of loading the normal content of that Page, the home page posts are loaded instead. The Loop displays them as if you were on the home page. That's sorta the point of that option.

    If your Page Template has a custom query_posts, then that, of course, will override the normal query. But if not, then yes, it will display the Posts when it's set to be the Posts Page. That's the whole point of that setting.

  3. jimmyjohn
    Member
    Posted 7 years ago #

    Otto - Thank you, but I'm still left confused here. As I said above, it was pretty clear that "Posts Page" made a given page into the Index. My question was relating to getting other pages using other templates to use the loop.

    The question is why additional pages, given the standard loop through a custom template, aren't loading The Loop. As I said in the post referenced above, the code I'm testing is very simple:

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

    <?php get_header(); ?>

    <div id="content">

    <?php include(TEMPLATEPATH."/l_sidebar.php");?>

    <div id="contentmiddle">
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    <?php the_excerpt());?>

    <?php endwhile; else: ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p><?php endif; ?>
    <?php posts_nav_link(' — ', __('« go back'), __('keep looking »')); ?>
    </div>

    <?php include(TEMPLATEPATH."/r_sidebar.php");?>

    </div>

    <!-- The main column ends -->

    <?php get_footer(); ?>

    As I said there, if I plug the same code into archive.php, it works; if I plug it into index.php, it works. It's only when I use this code as a custom template for a custom page that it just doesn't load The Loop (unless I set that page to essentially override the custom template by setting it as the Posts Page).

  4. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    As I said there, if I plug the same code into archive.php, it works; if I plug it into index.php, it works. It's only when I use this code as a custom template for a custom page that it just doesn't load The Loop (unless I set that page to essentially override the custom template by setting it as the Posts Page).

    Err... I think you misunderstand the whole purpose of the Loop. What you have above would just show the content of the Page, in your case. That's what it is supposed to do.

    Let's take the mystique out of "The Loop" for a second, shall we? The Loop is not special. It's just what it sounds like, a loop structure. Here's the main gist of it:

    while (have_posts()) : the_post();
    ...
    endwhile;

    Basically, while there are still "posts" to be displayed, it loads them, displays them, and loops back to display more posts. Simple, really.

    But note that "The Loop" doesn't actually *choose* which "posts" to display. This is determined elsewhere, by other things. In other words, The Loop doesn't actually *load* the posts, it just shows them on the webpage.

    So, why does your Loop show the Page content? Because, and this is important, a Page IS a Post. It's a post like any other, only it has special features. So your Loop, which is displaying posts, *IS* displaying posts. In this case, it's looping through one post: The Page. See?

    What you seem to want to change is to tell the thing what posts actually get loaded and displayed. This doesn't mean building a Loop. This means modifying the Loop by adding a query_posts() call in front of it.

    If you want to change the default query, you must have some way to specify exactly what query you really wanted. Otherwise, WordPress chooses for you, based on several things. In your case, you're loading a Page, so it assumes you want to have the Page in the Loop. When you load the index, it assumes you want the latest X posts. See? What part of the site you go to is what determines what "The Loop" displays, unless you specifically override that.

    Understand?

  5. jimmyjohn
    Member
    Posted 7 years ago #

    Otto - Understood. Many thanks!

  6. abcpeter
    Member
    Posted 7 years ago #

    Otto, adding a query_posts() into the Loop does the trick. But then the page navigation of a page that use this query_posts() is not working properly.

    Let me explain. I have a static front page, a Posts page and a page that have a query_posts() in its Loop. I want to use the default Posts page to show posts ordered by date and another page (the one with a query_posts()) to show posts ordered by title. So, in order to make the page display posts by title I include the following into the Loop:

    query_posts("cat=&orderby=title&order=asc");

    And when I visit the page I do see posts ordered by title, but when I click on the next page link (next_posts_link), I get referred to root.com/pageslug/page/2/, but all the results/posts are the same as on the previous page. So, no matter if you go on the next, or on the previous page, you always get the same results/posts.

    I use WordPress 2.2

Topic Closed

This topic has been closed to new replies.

About this Topic