WordPress.org

Support

Support » Themes and Templates » If page is a parent

If page is a parent

  • On my page I have:

    <?php if($post->post_child){?>
    <?php include (TEMPLATEPATH . "/menu.php"); ?>
    <?php }?>

    What I am hoping to achieve is that if the page is a parent or has a child (the same thing) then the menu is included. If not then it’s just blank.

    Thanks for your help in advance,
    Steve

Viewing 8 replies - 1 through 8 (of 8 total)
  • $children = get_children("post_parent=" . $post->ID );
    if( $children )
       // print menu
    <?php
    $children = get_children("post_parent=" . $post->ID );
    if( $children )
    {
    include (TEMPLATEPATH . "/menu.php");
    }
    ?>

    I’ve tried that above and it doesn’t work, it doesn’t show the menu template for the pages that have child pages. Just to recap – this is a parent page and I only want to include the menu template if the parent has child pages.

    Thank you,
    Steve

    This works for me 2.6.2:

    $children = get_children( "post_type=page" );

    This still includes the menu template even when the page has no children. Here is the full code including the contents of menu.php (instead of the include) incase what I have done in there is wrong.

    <?php
    $children = get_children( "post_type=page" );
    if( $children )
    {	<div class="Menu">
    
    <ul>
       <?php $pages = wp_list_pages('sort_column=menu_order&title_li=&echo=0&child_of='.$post->ID.'');
    $pages = preg_replace('%<a>]+)>%U','</a><a><span>', $pages);
    $pages = str_replace('</a>','</span>', $pages);
    echo $pages; ?>
      </ul>
    <? unset($pages); ?>
    </div>
    }
    ?>

    Thanks again,
    Steve

    Steve,
    Hi. I fudged around with this for a bit and got to the bottom of why the above examples are not working as expected. The latest versions of WordPress have introduced post revisions and this seems to be causing get_children() to return a “false” positive when there are no actual children pages. A page revision is stored in the database as a child page of the “actual” page with a post_type of “revision” and a post_status of “inherit”. I believe that the inherited post status is to blame here, but am not actually sure This being said, the following conditional code should print only when there are actual (non-revision) children”:

    <?php
    	$mf_children = get_children( 'post_parent=' . $post->ID . '&post_type=page&post_status=publish' );
    	print '<pre>'; print_r( $mf_children ); print '</pre>';
    	print 'COUNT: ' . count( mf_children );
    	print ' - ' . $mf_children;
    	print ' - ' . $post->ID;
    	if( count( $mf_children ) > 0  ) {
    		print '<div class="Menu" style="background:black;height:20px">';
    		print '<ul>';
    		$pages = wp_list_pages( 'sort_column=menu_order&title_li=&echo=0&child_of=' . $post->ID );
    		$pages = preg_replace( '%<a>]+)>%U', '</a><a><span>', $pages );
    		$pages = str_replace( '</a>', '</span>', $pages );
    		echo $pages;
    		print '</ul>';
    		unset($pages);
    		print '</div>';
    	}
    ?>

    However, this is not the case…
    Sorry, I do not have a solution in this particular case – prior to 2.6, the above code should work…

    Does anyone else have a view on this? I’m stumped!

    Guys just putting this out there again if anyone can answer it, I’m still not any further with the problem.

    I’m having the same problem. My post are http://myblog/archives/2250/ Then the next post will be http://myblog/archives/2252. Then the next is http://myblog/archives/2254. etc. Every post is given every other number. In my hosting account, however, in my phpAdmin, I see that WordPress is creating at least a second post of every post I create and labeling it “inherit.” So post 2250 has a copy at 2251 labeled “inherit,” and so on, and it is causing mySQL_slow_querie problems as instead of the 300 or so posts, myPHP shows 1900+ posts. Any suggestions? I am at a complete loss.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘If page is a parent’ is closed to new replies.