How to control display of page lists on sidebar that doesn't have childrens (3 posts)

  1. Libin
    Posted 4 years ago #

    Hi, 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.

  2. luckdragon
    Posted 4 years ago #

    if you don't want to display anything if there's no children, then you should move the initial $output line after the if ($children) then it will only create $output if ($children) is true

  3. Libin
    Posted 4 years ago #

    Thank You luckdragon. That worked.

Topic Closed

This topic has been closed to new replies.

About this Topic