Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using the following code to list the child pages of current page on my sidebar. It is working very well.

$titlenamer = get_the_title($post->post_parent);
$output = wp_list_pages('echo=0&depth=1&title_li=<div id="list-title-head">'.$titlenamer.'</div>' );
if (is_page( )) {
  $page = $post->ID;
  if ($post->post_parent) {
    $page = $post->post_parent;
  }
  $children=wp_list_pages( 'echo=0&child_of=' . $page . '&title_li=' );
  if ($children) {
    $output = wp_list_pages ('echo=0&child_of=' . $page . '&title_li=<div id="list-title-head">'.$titlenamer.'</div>');
  }
}
echo $output;

But If a parent menu has no childs, this code will list all the parent pages on my sidebar. How can I control this?

If the parent menu doesn't have any childs, then I wants to display certains pages I define. If this is not possible, I don't want to display anything for page that doesn't have childs.

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

I would try replacing your $children definition, using get_page_children() or get_children():

<?php
global $post;
$children = get_children( array(
    'post_type'   => 'page',
    'post_parent' => $post->ID,
    'post_status' => 'publish'
) );
if ( $children ) {
    // Code to list child pages goes here
}
?>

Also, wp_list_pages() will always return a string (either populated or empty), so if you're going to test against it, use:

if ( '' != $children )

...instead of just:

if ( $children )

Because the latter will always return true, since the return is an (empty) string.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.