Support » Plugins » If (this page has subpages) then

  • I have some pages that have subpages and I want to show links to the suppages on those in a (good looking) div. BUT(!) I don’t want the div to show on those pages that doesn’t have subpages.

    I would like to think that it could be easily done if there just were a conditional tag like “is_parent”.

    Something like:

    <?php
    if(is_parent()){
    ?>
    <div id="goodlooking">
    <?php
    wp_list_pages()?>
    
    </div>
    <?php
    }
    ?>

    I hope you understand what I’m after.
    Happy for help.

Viewing 15 replies - 1 through 15 (of 16 total)
  • You would have to determine if the page has children, then set up a conditional for the pages template.

    Try this, works on my site http://RogerTheriault.com/
    I put it in my pages.php template.
    Note the child_of option to wp_list_pages, that’s critical. You may want to check out the other options if you need more customization.

    <?php if (!is_category() && !is_archive()) {
    // add subpage list if single page
    $children = wp_list_pages('title_li=&echo=0&child_of=' . $post->ID);
    if ($children) { ?>
       <div class="mychildren">
       <h2>Inside This Section:</h2>
       <ul>
       <?php echo $children; ?>
       </ul>
    <?php }
    } ?>

    johdan,

    Did you find an answer to this?

    I’m trying to do same. I’ve created a horizonal menu. Parent categories have dropdowns showing the children. HOwever, I want to style the parent links so they have a little downward arrow next to them marking them as parents.

    I have not found a is_parent conditional, yet.

    AdamM

    RogerTheriault: is there any way to modify that code (which works perfectly for me!) to display the same information on the child pages also?

    @kalikat – yes, replace $post->ID with $post->post_parent and you should generate a list of siblings (“brothers and sisters”) of the post you’re on.

    See here for more attributes of the $post object: http://codex.wordpress.org/Function_Reference/get_post although template tags are preferred (if they exist) for easier upgrading.

    Notice that on my site, I used some CSS styling to show the current section of my site in the right sidebar. I didn’t have to write code, just a bunch of CSS instead. I’m not sure how useful my site visitors find it but it’s an alternative (if you have 2.5 or up, since some of the styles are new).

    @adamm – Try doing it with CSS, you can style a div that has an embedded div differently than one that does not.

    @roger – thanks so much!

    I’m having a bit of trouble getting the code to work. I’m still a bit confused by the $post->post_parent relationship. Basically, I want my sub-pages to show a list of their children whilst on that sub-page or on any of its children, ignoring a top-level of pages entirely.

    Page (no listing of pages)
    -Sub-page (list of all pages under this page)
    –Sub-sub-page (list of all siblings -i.e. identical to that above)

    $post->ID works on my sub-pages, but shows nothing once in the sub-sub-page and $post->post_parent works on my sub-sub-pages, but shows an entire list of my pages on the sub-page. It’s very frustrating!

    This codex example sounds like it should work:

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

    …but, again, only works once drilled down into a sub-sub-page. On a sub-page, it shows all siblings of that page and their children. It’s like I need a combination of your code and the codex example working in tandem, but I can’t seem to make the thing work at all!

    @kalikat

    $post->ID works on my sub-pages, but shows nothing once in the sub-sub-page and $post->post_parent works on my sub-sub-pages, but shows an entire list of my pages on the sub-page. It’s very frustrating!

    Remember ID and post_parent are parameters for the “child_of” attribute. ID = current post and therefore you’ll get all the children of the current post using ID. post_parent is the id of the current post’s parent, so you’ll get all of the parent’s children, which will include the current post and its “brothers and sisters”.

    If you look at it in a family tree sort of way, infants can’t have children of their own, which is why you’re getting nothing on your sub-sub-pages.

    Hi,

    is there a solution for this. I have exactly the same problem.
    I want to show Subpages in the sidebar as long as there are some, if there are no more subpages on a level, i want to show the pages that are on that level.

    Thanks for your help

    hi – yeah, i figured this out while failing to figure out something else

    <?php
    global $wp_query;
    
     if ( empty($wp_query->post->post_parent) )
     { ?> this text displayed if page is a parent <?php } else { ?> this text displayed if page is a child <?php } ?>

    alternatively, if you’re doing navigation which is mainly the question here (i’m using the fold page plugin which is why the call ( wswwpx_fold_page_list ) looks funny) you can do something like this (see below) – check out the “Navigation” area here: http://www.smallpointclub.com/?page_id=59 | the effect is that the user can click up and down the sub pages and see where they are in relation to the page-parent and the other page-children

    <?php
    global $wp_query;
    
    if( empty($wp_query->post->post_parent) ) {
    $parent = $wp_query->post->ID;
    $parent_title = $wp_query->post->post_title;
    } else {
    $parent = $wp_query->post->post_parent;
    $parent_title =
    $wpdb->get_var("SELECT post_title FROM $wpdb->posts WHERE ID = $parent");
    }
    ?>
    
    <h2><a href="http://fernald4portland.com/?page_id=<?php echo apply_filters('the_title', $parent); ?>">"<?php echo apply_filters('the_title', $parent_title);?>"</a></h2>
    
    <ul id="mininav">
    <?php wswwpx_fold_page_list ("&sort_column=menu_order&title_li=&child_of=$parent"); ?>
    </ul>

    CSS to style this:

    /* Begin Mini Nav */
    
    #mininav {
            padding: 8px;
            width: 150px;
            float: right;
            margin-left: 20px;
            margin-bottom: 8px;
    
    }
    
    #mininav li, #mininav ul {margin-left: 0px; padding-left: 0px; list-style: none;}
    
    #mininav a {
            font-size: .8em;
            color: #222;
            text-decoration: underline;
            margin-left: 0px !important; padding-left: 0px !important;
    }
    
    #mininav a:hover {
            color: #600000;
            text-decoration: none;
    }
    
    #mininavnews a {
            font-size: .7em;
            color: #222;
            text-decoration: underline;
            margin-left: 0px !important; padding-left: 0px !important;
    }
    
    #mininavnews a:hover {
            color: #600000;
            text-decoration: none;
            margin-left: 0px !important; padding-left: 0px !important;
    }
    
    #mininav li.current_page_item a {
            color: #600000;
            text-decoration: none;
            margin-left: 0px !important; padding-left: 0px !important;
    }
    
    #mininav li ul {
            margin: 0px 0px 0px 5px;
            padding: 0px;
            list-style: none;
            margin-left: 0px !important; padding-left: 0px !important;
    }
    
    #mininav li ul li.page_item a {
            color: #000;
            text-decoration: underline;
            list-style: none;
            margin-left: 0px !important; padding-left: 0px !important;
    }
    
    #mininav li ul li.page_item a:hover {
            color: #600000;
            text-decoration: none;
            margin-left: 0px !important; padding-left: 0px !important;
    }

    Hey,

    So I want to have a conditional that includes a file if the page has children – not to display those children as discussed above. If the page does not have children I want to include a different file.

    So plain spoken program:

    If page is parent {

    include(file-for-parent-pages.php);

    } else {

    include(file-for-non-parent-pages.php);

    }

    Could someone please provide this snippet.

    I have tried this:

    <?php
    global $wp_query;

    if ( empty($wp_query->post->post_parent) )

    { include(‘side-parent.php’); }

    else

    { include(‘side-no-children.php’); }

    ?>

    THANKS!!!

    Why not adapt the code, that’s on the wp_list_pages codex page?

    <?php
    if($post->post_parent)
    	include(file-for-parent-pages.php);
    else
    	include(file-for-non-parent-pages.php);
    <?php }?>

    Of course, I didn’t try this so it might be totally screwed up.
    But I

    Did this get solved?

    I am using this to get variable sidebar content:

    <?php
    global $post;

    if (is_page() && $post->post_parent ) {
    include(‘sub-page.php’);
    } else {
    include(‘not-sub-page.php’);
    }
    ?>

    But this only helps me if the page is a sub_page.

    I want something exactly the same that works if the page is a parent

    For anyone still trying to solve this, this site helped me solve this problem: http://www.blogherald.com/2007/07/26/wordpress-dynamic-sub-page-navigation/.
    Hope it helps….

    ojster

    (@ojster)

    Well I am trying to achieve the same thing, but with no luck.

    I want to list subpages (children) on the sidebar if I am on the parent page and when I am on the child page I want to display the same list (brothers) if it does not have any more children. If it has children, then they should be displayed.

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

    The above code works to display children if the parent has them, but it fails to display brothers when the page has no children. I searched the forums and documentation and I googled and I can not find a solution.

    Any ideas? It seams like this else function does not do anything at all. I tried to display just a certain category ID list, but it also did not function. How would the else statement be coded correctly?

    Thanks a lot!
    Peter

    ojster

    (@ojster)

    I guess the basic question is how to get this if function working properly. Then I think the list_pages should actually work the way it is written above?

    I tried also the code in the following way:

    <?php if (have_posts()) : ?>
                   <?php while (have_posts()) : the_post(); ?>
    TEST
         <?php endwhile; ?>
         <?php else: ?>
    TEST1
         <?php endif; ?>

    I guess it is showing I am new in PHP 🙂 any ideas why this function does not work?

    I assume this have_posts() check in overall pages and not just if this page has children as it always just displays the first value and never the second… How could I check if this certain page has children. So have_posts() for that specific page? I also tried is_parent() instead but it generates the same results…

    thanks!

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘If (this page has subpages) then’ is closed to new replies.