• Someone posted this code, which when I paste it in the Page Template (page.php) works to make all of the pages display on one page (the parent page). Depending where in the template I paste it, it has a different influence (it seems to interfere with css). I am not sure where to paste it so all of the pages are presented in their original style (how they appear when I leave them as separate pages which I would load from the drop down menu). I’m currently using the Twenty Thirteen page template :

    <?php $pages = get_pages();
    foreach ($pages as $page_data) {
    $content = apply_filters(‘the_content’, $page_data->post_content);
    $title = $page_data->post_title;
    $slug = $page_data->post_name;
    echo “<div class=’$slug’>”;
    echo “<h2>$title</h2>”;
    echo $content;
    echo “</div>”;
    }
    ?>

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    The problem is this loop isn’t generating the appropriate classes for the CSS to work properly. If it did that, it may not matter as much where the code was placed. After making the changes shown below, try inserting the whole thing after <?php endwhile; ?>. Though if you want to avoid the duplicated entry you could actually replace the main loop with this code.

    Which means the original query is tossed and get_pages() is used instead. Which begs the question, why not query all the pages in the first place.

    Anyway, back to your question. To generate the correct classes, etc, mimic the main page loop. Replace the four echo lines with this code adapted from the main loop:

    ?><article id="post-<?php $page_data->ID; ?>"
        <?php echo 'class="' . join( ' ', get_post_class( '', $page_data->ID ) ) . '"'; ?>>
    	<header class="entry-header">
    		<h1 class="entry-title"><?php echo $title; ?></h1>
    	</header><!-- .entry-header -->
    	<div class="entry-content">
    		<?php echo $content; ?>
    	</div><!-- .entry-content -->
    	<footer class="entry-meta">
    		<?php edit_post_link( __( 'Edit', 'twentythirteen' ), '<span class="edit-link">', '</span>', $page_data->ID); ?>
    	</footer><!-- .entry-meta -->
    </article><!-- #post --><?php

    There may be need for more tweaks, but it’s at least a start.

    Thread Starter nachoroberto98

    (@nachoroberto98)

    Thank you very much for your help! This is working very well so far. A couple of hiccups though. The first is the pages are appearing in alphabetical order of the page titles rather than in order of the page numbers I assigned them (as they should appear after the parent page). The second is the parent page appears twice. I’m not sure which lines of the code to get rid of

    Also, I am not sure how to query all of the pages instead. What would the advantages be?

    Thanks for bearing with me. I am learning a lot but this is my second day learning about editing php.

    Thread Starter nachoroberto98

    (@nachoroberto98)

    Basically I want the page to read through like a Textbook, with multiple Headers which can be accessed not only from the drop down menu, but also have their own url which brings someone directly to that part of the static page.

    Moderator bcworkz

    (@bcworkz)

    Alphabetical? That’s odd, the default is publish date. No matter, where are the assigned page numbers stored?

    This really is sounding like making the correct initial query would be a good approach. The main advantage is it’s more efficient, WordPress does not waste time and resource doing a query which is essentially thrown out unused and replaced with another that also uses up time and resource. The drawback is it can be more difficult to implement. Calling get_pages() and running a loop is pretty easy.

    Funny thing is, I’m not quite sure of the best approach. WP can query all posts without us doing anything. But all pages? Kind of different. Let’s try this. Hook the ‘pre_get_posts’ action and check if the query var ‘pagename’ contains the slug for what ever page you are currently using to display all pages. If so, set or unset various query vars as needed as to create the proper query. You will unset ‘pagename’ because you want more than just the record corresponding to that one slug. You set other query vars in a way similar to WP_Query arguments. If a WP query argument was array('orderby'=>'author') you would set the ‘orderby’ query var to ‘author’.

    Besides unsetting the pagename, the only thing you may need to set is the page number order, how depends on where it is stored. This should result in all pages displayed on a single page any time that one page slug is requested, and everything else will continue to work as expected. You would also remove that extra code to the page template so it is restored to it’s default state. All the pages will be produced from the original loop, the added code is not needed. This also facilitates requesting individual pages by individual slugs without getting all pages.

    The skip link functionality from a drop down will be accomplished by linking to each post-id that is output in the <article> tag. The href of links would look something like "#post-234" where 234 is the post ID of a particular page.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to make all Pages display on one page while retaining style and menu’ is closed to new replies.