Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Parent Page and Child Pages, works but needs a tweak on the code WordPress

  • SOLVED

Hello,

I got this code from here - [[LINK href="http://www.martin-gardner.co.uk/how-to-list-wordpress-child-pages-only-on-that-parent-page/"]]How To List WordPress Child Pages Only On That Parent Page[[/LINK]]


<?php
if( is_single() ) {

$this_category = get_the_category($cat);
$parent_category = get_the_category($this_category->category_parent);
$getParentCatId = $parent_category->cat_ID;

wp_list_categories('depth=2orderby=id&title_li=<h2>More Categories</h2>&child_of='.$getParentCatId);
}
if( is_page() ) {
$category = get_the_category();
$catname = $category[0]->cat_name;
$category_id = get_cat_ID($catname);

if($post->post_parent)
$children = wp_list_pages('sort_column=menu_order&title_li=&child_of='.$post->post_parent.'&echo=0');
else
$children = wp_list_pages('sort_column=menu_order&title_li=&child_of='.$post->ID.'&echo=0');
if ($children) {
echo "<ul>";
echo $children;
echo "</ul>";
}
}
?>


Now it works great but when you click on any of the child pages all the post show up towards the bottom

Site: [[LINK href="http://testsite.ledesmaweb.com/"]]Test Site[[/LINK]]

Go [[LINK href="http://testsite.ledesmaweb.com/essays/"]]here[[/LINK]] which is my Essay page(the parent page) and click on any of those three post and towards the bottom, you'll see the exact list repeated.


The goal of this question is to remove that list from the individual child pages. The child pages should only be themselves. The list of child pages should only show up on the parent page which is called Essays.

And its not a pain, create a class so I can style those items.

Answers (1)

2011-01-30

Chris Lee answers:

You need to restrict it to only child pages


<?php

if( is_single()) {



$this_category = get_the_category($cat);

$parent_category = get_the_category($this_category->category_parent);

$getParentCatId = $parent_category->cat_ID;



wp_list_categories('depth=2orderby=id&title_li=<h2>More Categories</h2>&child_of='.$getParentCatId);

}

if( is_page() ) {

$category = get_the_category();

$catname = $category[0]->cat_name;

$category_id = get_cat_ID($catname);



if($post->post_parent)

$children = wp_list_pages('sort_column=menu_order&title_li=&child_of='.$post->post_parent.'&echo=0');

else

$children = wp_list_pages('sort_column=menu_order&title_li=&child_of='.$post->ID.'&echo=0');

if ($children && is_page('essays')) {

echo "<ul>";

echo $children;

echo "</ul>";

}

}

?>


Jorge Ledesma comments:

That did not do anything, if you like at the Essay page, it shows the code, kind of weird


Chris Lee comments:

Sorry I edited it now. Give it a shot. i've limited to only display on the essay page


Jorge Ledesma comments:

Parse error: syntax error, unexpected '{' in /home1/nokiamob/public_html/testsite/wp-content/themes/journalist/page.php on line 88


Chris Lee comments:

And that time i forgot a parentheses. Corrected


Chris Lee comments:

Looks like it works! And if you need to add any more pages. add more page names


Jorge Ledesma comments:

excellent. anyway to show the published dates like in the archives.


Chris Lee comments:

I could do that but that's another question.


Jorge Ledesma comments:

thanks