• Resolved strat435

    (@strat435)


    Hello

    I’m teaching myself WordPress by starting with the blankslate template and converting a previously designed website into a WordPress based site. One of the problems I haven’t been able to figure out is pulling content through different templates onto a single display page.

    So, I have my Home page which is running a certain template and pulling content from a Static Page. I’d like to drop in another template that will pull content from a few other Static Pages (which are subpages of the Home page).

    I’ve tried get_post($id)->post_content but that pulls only the sub-page content without running it through the template. (The correct template is assigned to the sub-pages). I’d also prefer to do it programtically by targeting all sub-pages rather than manually calling them by id.

    I was also looking at get_template_part('sub-page-template') but I don’t understand how to run the correct query to pull the correct sub-page content from the database and have it run through the template.

    Thank you!

Viewing 3 replies - 1 through 3 (of 3 total)
  • esmi

    (@esmi)

    I’d suggest that you start reading up on WP_Query() and multiple Loops.

    Thread Starter strat435

    (@strat435)

    Thanks! So using this Query Generator I’m trying out the following code:

    <?php
    
    $args = array (
    	'post_parent'            => '70',
    	'order'                  => 'ASC',
    	'orderby'                => 'menu_order'
    );
    
    // The Query
    $query = new WP_Query( $args );
    
    // The Loop
    if ( $query->have_posts() ) {
    	while ( $query->have_posts() ) {
    		$query->the_post();
    		?>
    		<div class="content-front-page-i-cont" id="<?php echo the_title(); ?>">
    				<?php the_content(); ?>
    			</div>
    		<?php
    	}
    } else {
    	?>No posts found<?php
    }
    
    // Restore original Post Data
    wp_reset_postdata();
    
    ?>

    This doesn’t retrieve any posts. If I do page_id instead of post_parent I can retrieve the parent page. I double checked and there are several pages that are using the queried page as a parent.

    Thread Starter strat435

    (@strat435)

    After some investigating, I was able to figure out when using 'post_parent' => 'id' you also need to use 'post_type' => 'page'. Adding that additional argument allowed me to display those pages.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘displaying subpages on a page using different templates’ is closed to new replies.