Multiple pages on one with different HTML

You could attack the get_pages(); function another way to sort your code better and read it better as well, take a look at this link for more info.

<?php $args = array(
    'sort_order' => 'ASC',
    'sort_column' => 'post_title', // sorting by alphabetical order of post title for example
    'hierarchical' => 1,
    'exclude' => '',
    'include' => '',    // edit this parameter instead of $pages = get_pages('include=2,4,5,8');
    'meta_key' => '',
    'meta_value' => '',
    'authors' => '',
    'child_of' => 0,
    'parent' => -1,
    'exclude_tree' => '',
    'number' => '',
    'offset' => 0,
    'post_type' => 'page',
    'post_status' => 'publish'
); //edit whatever parameter you need to get what you need (sort_column to select which column to sort by), this list shows all parameters!!!

$pages = get_pages($args); 

    foreach ($pages as $page_data) {
        $content = apply_filters('the_content', $page_data->post_content);
        $title = $page_data->post_title;
        $slug = $page_data->post_name;

        if( $page_data->ID == 2 ){ //format 1
            get_template_part( 'content-1', get_post_format() ); //get post template from content-1.php
            }
        elseif( $page_data->ID == 4 ){ //format 2
            get_template_part( 'content-2', get_post_format() ); //get post template from content-2.php
            }

        }
?>

I presented a variation for the content of the if statement in case the code that displays each section gets too big, you can simply break it into other files to control it with ease and make it more readable. More usage about the get_template_part(); function refer to this link.

Leave a Comment