0

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.

1 Answer 1

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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