最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Multiple pages on a single page

programmeradmin1浏览0评论

I have the following code (created very kindly by alchemyth in response to a question I posted on the WordPress 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.

I have the following code (created very kindly by alchemyth in response to a question I posted on the WordPress 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.

Share Improve this question edited May 4, 2014 at 18:38 martin asked May 4, 2014 at 18:12 martinmartin 1131 gold badge1 silver badge7 bronze badges 0
Add a comment  | 

2 Answers 2

Reset to default 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.

This works for me with WP Twenty Twelve, don't forget to change page IDs

 <?php
/*
Template Name: My Custom Page
*/

get_header();
$args = array(
    'posts_per_page' => get_option('posts_per_page'), // значение по умолчанию берётся из настроек, но вы можете использовать и собственное
    'paged'          => $current_page // текущая страница
    );
    query_posts(
    array( 'post_type'=>'page', 'post__in' => array( 6891, 9400 ) ) //change page IDs
); ?>

<div id="primary" class="site-content">
    <div id="content" role="main">
<?php
            while ( have_posts() ) :
                the_post();
                ?>
                <?php if ( has_post_thumbnail() ) : ?>
                    <div class="entry-page-image">
                        <?php the_post_thumbnail(); ?>
                    </div><!-- .entry-page-image -->
                <?php endif; ?>

                <?php get_template_part( 'content', 'page' ); ?>

                <?php endwhile; // end of the loop. ?>

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

<?php get_sidebar(); ?>
<?php get_footer(); ?>

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论