• Is there a way to show the full sub-page content (not a link) on a parent page? An example is a food product with nutritional data. I prefer that nutritional data is added as a subpage as a different post, then shown on the product page. This sub page content could be placed in the sidebar or wherever. Thanks for any ideas.

Viewing 5 replies - 1 through 5 (of 5 total)
  • kevo, I’m looking for a similar solution… any luck?

    I just googled that exact phrase…. Still no luck?

    <?php $pages = get_pages('child_of='.$post->ID.'&sort_column=post_date&sort_order=desc&parent='.$post->ID); $count = 0; foreach($pages as $page) { $content = $page->post_content; if(!$content) continue; if($count >= 2) break; $count++; ?>
    
       	<div class="feature_product">
      <a href="<?php echo get_page_link($page->ID) ?>"><h2><?php echo $page->post_title ?></h2></a></div>
    
        <?php
        }
    ?>

    Hope that helps! I only call the title in this example. But you can also call the content within the “feature_product” div!

    Pardon my ignorance (my first attempt and wordpress). I’m assuming that the code is added to the page.php file. Where does it go?

    Thanks in advance.

    Based on the code posted by krusch, here’s what I ended up with:

    <?php
    	$pages = get_pages('child_of='.$post->ID.'&sort_column=post_date&sort_order=desc&parent='.$post->ID);
    	foreach($pages as $page) {
    ?>
    
    		<div class="section" id="<?php echo $page->post_name; ?>">
    			<h4><a href="<?php echo get_page_link($page->ID) ?>"><?php echo $page->post_title ?></a></h4>
    			<?php echo apply_filters('the_content', $page->post_content); ?>
    		</div>
    
    <?php } ?>

    The code from krusch stopped after 2 items, so I removed that logic.
    I also changed the class on the wrapping div to “section” rather than “feature_product” since it made more sense in the context of my particular usage (but it’s semantically generic enough for general use). I also added an id to the wrapping div – this id is based on the ‘URL slug’ by using ‘$page->post_name’.

    Also notice that I changed the H2 to an H4 (only because it made sense in the context of my usage), but please also notice that the code from krusch wraps an anchor (‘a’) element around the H2 which is invalid HTML, so I instead wrapped the H2 (H4 in my case) around the anchor.

    As krusch suggests, you can also call the content which I originally did using just $page->post_content. That worked, but it needed formatting, so I ran it through the apply_filters() function like so: apply_filters(‘the_content’, $page->post_content);

    Hope this helps. And to answer mortocks’ question – yes, this code is added to page.php

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘show sub page content on a parent page’ is closed to new replies.