I have a site that is using a hierarchical page structure.
Example:
Widgets
> Widget 1
> Widget 2
> Widget 3
The parent page is actually empty but I want to be able to show the title/content of the sub-pages when you visit the "Widgets" page. Pretty much faking the functionality of a category archives template.
Sébastien Méric answers:
Hi,
This is the kind of page template i use to do that :
<?php
/**
* Template Name: Page with it's subpages content
*/
get_header();
get_sidebar( 'left' );
// Preserve original wordpress query
$temp_query = $wp_query;
// Custom query
$args = array(
'post_type' => 'page',
'orderby' => 'menu_order',
'order' => 'ASC',
'post_parent' => $post->ID,
'post_status' => 'publish',
'posts_per_page' => 9999,
);
$wp_query = new WP_Query( $args );
?>
<div id="content-wrapper">
<div id="content">
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID() ?>">
<h2 class="entry-title"><?php the_title() ?></h2>
<div class="entry-content"><?php the_content() ?></div>
</div>
<?php endwhile ?>
<?php else : ?>
<div <?php post_class() ?> id="post-0">
<h1 class="entry-title"><?php _e( 'Not Found' ) ?></h1>
<div class="entry-content">
<p><?php _e( 'Sorry, but you are looking for something that isn’t here.' ) ?></p>
</div>
</div>
<?php endif ?>
</div><!-- /#content -->
</div><!-- /#content-wrapper -->
<?php
// Reverse to original wordpress query
$wp_query = $temp_query;
get_sidebar( 'right' );
get_footer();
?>
Just duplicate your index.php and replace its content with that. Name it something like page-with-subpages.php then, in your parent page in the back office, choose it as a template...
Hope it helps :)
Séb.
Maor Barazany answers:
I've done such a thing before, here's my solution that works fine.
It lists also the "brother" pages in each page that has a brother.
Put this in your <strong>page.php</strong> file, it shows the actual menu of bothers pages:
<?php
$children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
if ($children || $post->post_parent != 0) : ?>
<div class="children">
<?php list_page_childs();?>
</div>
<?php endif;?>
Also, put this function in your <strong>functions.php</strong> of your template, it make the code above to work...
function list_page_childs() {
global $post;
$current = $post->ID;
$parent = $post->post_parent;
$get_grandparent = get_post($parent);
$grandparent = $get_grandparent->post_parent;
$children = wp_list_pages('title_li=&child_of='.$current.'&echo=0&depth=1&sort_column=menu_order&link_before=<span></span>');
$siblings = wp_list_pages('title_li=&child_of='.$parent.'&echo=0&depth=1&sort_column=menu_order&link_before=<span></span>');
//main is the first item listed, show always the ancestor,
//decide who is the right ancestor and assign to $main
if ($parent == 0) { $main = $current; }
elseif ($grandparent == 0) {$main = $parent;}
else {$main = $grandparent; }
?>
<ul>
<?php
$main_li_item = wp_list_pages('title_li=&echo=0&include='.$main.'&depth=1&link_before=<span></span>');
//if in the grandfather
if ( $parent == 0 ) echo $main_li_item . $children;
//in one of the children - show their brothers.
elseif ($grandparent == 0) echo add_main_class($main_li_item) . $siblings;
//in the granchild - show the parent and uncles (children of the grandparen)
else {
echo add_main_class($main_li_item);
wp_list_pages('title_li=&child_of='.$grandparent.'&depth=1&sort_column=menu_order&link_before=<span></span>');
}
?>
</ul>
<?php
} //list_page_childs
function add_main_class($input) {
if( $input ) {
$pattern = '/<li class="/is';
$replacement = '<li class="main ';
$input = preg_replace($pattern, $replacement, $input);
return $input;
}
}
Maor Barazany comments:
Another way to implement this if you want, put this in your <strong>page.php</strong> file, in the place you want the children pages to show:
<div class="children">
<?php
$children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0&sort_order=ASC');
$siblings = wp_list_pages('title_li=&child_of='.$post->post_parent.'&echo=0&sort_order=ASC');
if ($post->post_parent != 0) { ?>
<span>More <a href="<?php echo get_permalink($post->post_parent); ?>" rel="bookmark"><?php echo get_the_title($post->post_parent);?></a>:</span>
<?php }
if ( $children || $post->post_parent != 0) { ?>
<ul>
<?php echo $children;
if ($post->post_parent != 0) echo $siblings; ?>
</ul>
<?php } ?>
</div> <!--children-->
Maor Barazany comments:
My solution, by te way, is general and not for a specified page. Also, it will give you the listing also on all brother pages of certain page, so if you view page "widget 2", you will see its brothers as well.
If a page doe not have children, it won't show a thing.
Joe Calithia comments:
I was already listing the children page titles/links but I need to show the title/content etc. Not just a <li> of pages.
Maor Barazany comments:
Ok, so put this into your <strong>functions.php</strong>
p
function list_page_childs() {
global $post;
$current = $post->ID;
$parent = $post->post_parent;
$args = array(
'post_type' => 'page',
'numberposts' => -1,
'post_parent' => $parent,
);
$children = get_posts($args);
if ($$children) {
foreach ($children as $post) {
setup_postdata($post);
the_title();
the_content();
}
}
?>
Of course, you can add styling to <strong>the_title();the_content();</strong>, to show it in the style you need..
In your page.php just call the function by -
list_page_childs()
Joachim Kudish answers:
Hi,
This is exactly what I did here: http://fellerath.com/clientlogin/
It's very easy, all you have to do is get the page ID of the parent (in your case Widget) and list it's child page. Just add the following code where you want it in your template files
<?php wp_list_pages("title_li=&child_of=857" ); ?>
simply replace 857 with the ID of your parent page above.
Feel free to ask additional details.
Best of luck
------
Update: Ah I see; I misunderstood the original question. Well the one below is exactly right.
Cheers,
Joey
Joe Calithia comments:
I was already listing the children page titles/links but I need to show the title/content etc. Not just a <li> of pages.
So if you go to the "Widgets" page you will see an archive list of all it's sub-pages with title/meta/excerpt/etc just as you would if these were categories.
Daniel Wiener answers:
I do exactly the same thing as Sébastien Méric. I know I don't get to "win" the money... but I thought I would "vote" for an answer. It is pretty simple.