• Hello there,

    I’m currently trying to get accustomed to WordPress, and in order to do so I’m trying to convert one of my existing designs into a WordPress theme.

    I have one horizontal navigations which displays all the parent pages. And I have a vertical navigation displaying all the subpages, if existing.
    However, I’d like to use two different site templates, one with the vertical navigation and one without it.

    In order to find out whether a page is a subpage, I use code snippet 2 from here.

    Now I still need to find out whether the current page has subpages…unfortunately I’m not quite sure how to do that. Any help for a helpless WP newbie? ??

Viewing 9 replies - 1 through 9 (of 9 total)
  • What the code snippet does is check if the current page has a parent page and returns the value. You could do a check then with a simple if statement like:

    if(is_subpage()){
    do something
    }
    else { do other things }

    Hope this helps. ??

    Thread Starter lenuam

    (@lenuam)

    Hey, thanks for your reply.
    I already understood the code snippet, though :-)…

    I’ll try to describe it a bit better:

    I want my vertical navigation (displaying only subpages) to appear in two cases:

    1. The page is a subpage.
    2. The page has subpages.

    1. is accomplished by the code snippet, but I’m having a hard time trying to figure out how to accomplish 2.

    Of course I could always include the vertical navigation for specific pages (e.g. if (is_page(‘about’)) ), but I’d prefer to do it dynamically…

    Thread Starter lenuam

    (@lenuam)

    Anyone?

    1. is accomplished by the code snippet, but I’m having a hard time trying to figure out how to accomplish 2.

    Try below code.

    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){
      ...do something
    }

    Thread Starter lenuam

    (@lenuam)

    Hey, thanks so much, it works perfectly ??

    Drat! that is not working for me at all ??

    In my case, I want to show a heading followed by the list of children (of pages, not posts); and I want to hide the heading if page has no child pages.

    I can already hide/show the children, but I want to do nearly what Lenuam is doing except I just want to return true or false if the page has children. So I took the code above and tried to create a function out of it:

    function has_subpage(){
    	if( is_page() && $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){
    	  return true;
    	}else{
    	  return false;
    	};
    }

    Unfortunately, all I ever get returned is a 1 even when the page has not children. Here’s what I’m working on (please ignore the formatting — I’m just trying to get the logic in before applying a template and style):

    Top of the right margin, it will say “Pages within BBQ”, after which is a “Subpage value: #” where I’m testing the function.

    Now click Lodging on the global nav — Lodging has no child pages. The Subpage value still reads 1. Why wouldn’t it read 0?

    Thanks!
    -Sigo

    Oops. Forgot to close the anchor tag. Sorry!

    Actually, I found the answer on Template Tags/wp list pages

    Sick posting. Thanks!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Test if page has subpages’ is closed to new replies.