1

I have the following code (created very kindly by alchemyth in response to a question I posted on the WordPress.org forum here) that I hope to use to display lots of pages on a single page:

<?php
$args = array(
  'post_type' => 'page',
  'post__in' => array( 2, 5, 35, 67 ) //list of page_ids
);
$page_query = new WP_Query( $args );
if( $page_query->have_posts() ) :
echo '<div class="pages-on-page">';
//print any general title or any header here//
while( $page_query->have_posts() ) : $page_query->the_post();
echo '<div class="page-on-page" id="page_id-' . $post->ID . '">';
//print any output you want per page//
echo '</div>';
endwhile;
echo '</div>';
else:
//optional text here is no pages found//
endif;
wp_reset_postdata();
?>

As a newby though, I am unsure where to put it to make it work! I would be very grateful if someone could help me with this.

2

Take a look at Wordpress Page Templates;

You can create a file with structure to match your current theme and place your code in the content area ( or place an action hook for your special content ).

Example for Twenty Twelve:

/*
Template Name: My Custom Page
*/

get_header(); ?>

<div id="primary" class="site-content">
    <div id="content" role="main">

        <?php
        $args = array(
          'post_type' => 'page',
          'post__in' => array( 2, 5, 35, 67 ) //list of page_ids
        );
        $page_query = new WP_Query( $args );
        if( $page_query->have_posts() ) :
        echo '<div class="pages-on-page">';
        //print any general title or any header here//
        while( $page_query->have_posts() ) : $page_query->the_post();
        echo '<div class="page-on-page" id="page_id-' . $post->ID . '">';
        //print any output you want per page//
        echo '</div>';
        endwhile;
        echo '</div>';
        else:
        //optional text here is no pages found//
        endif;
        wp_reset_postdata();
        ?>

    </div><!-- #content -->
</div><!-- #primary -->

****Do not forget to start the file with < ? php

Save this code as a PHP file with almost any name your_custom_template.php and place it into your child theme folder. Then add a new page and assign the page template as My Custom Page as seen above in the code.

That should get you at least started enough to see your code on the front of the website.

  • I have put the code into a page using a php plugin using 'default template' and 'no parent' - not working though - what am I doing wrong? – martin May 4 '14 at 18:23
  • I would first need to know what plugin you are talking about. But before that, have you looked at the Wordpress Codex link I gave in my answer? – Douglas.Sesar May 4 '14 at 18:27
  • The plugin is here. And yes, I did look, but I must confess an awful lot of it goes over my head - I will persevere! – martin May 4 '14 at 18:30
  • What is the theme you are using? I may be able to get you started with an example template. – Douglas.Sesar May 4 '14 at 18:40
  • 2
    You need to add the code for the output //print any output you want per page// – Brad Dalton May 4 '14 at 21:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.