user3242861
user3242861

Reputation: 1929

Wordpress - get pages in same page

I'm creating a wordpress top-down website by sections and i don't know which is the best way to get the pages into different sections. I also want to take into account the charging performance

<section id="about">
  <!-- Content of page -->
</section>
<section id="services">
  <!-- Content of page -->
</section>
<section id="contacts">
  <!-- Content of page -->
</section>

Thank's

Upvotes: 0

Views: 375

Answers (1)

MrZiggyStardust
MrZiggyStardust

Reputation: 733

I would use a simple WP_Query instance here, and use the following code:

<?php 

// Set the WP_Query arguments
$args = array(
    'post_type' => 'page',

    // You need to change this to match your post IDs
    'post__in' => array( 10, 20, 30 ),
    'order_by' => 'post__in',
    'order' => 'DESC' // Might need to change this to "ASC"...
);

// Create the page section query
$page_section_query = new WP_Query( $args );

// Simple check...
if( $page_section_query->have_posts() ) :
    while( $page_section_query->have_posts() ) :
        $page_section_query->the_post();
        global $post;

        // Print out the section!
        ?>
        <section id="<?php echo esc_attr( $post->post_name ) ?>" <?php post_class( array( esc_attr( 'page-section-' . $post->post_name ) ) ); ?>>
            <!-- contents of the page section -->
        </section>
        <?php
    endwhile;

    wp_reset_postdata();
endif;
?>

Simple and effective, 1 query is all that this needs. Just keep adding more post IDs if you want more sections.

If you want to avoid using post IDs as an ordering parameter you could use: menu_order instead. And if you would like to avoid using post__in parameter you could add all the pages to a page parent and use the parent post ID and get all the page children for the sections. This would make the solution a little more modular.

Read more about WP_Query here: https://codex.wordpress.org/Class_Reference/

Upvotes: 2

Related Questions