find if a wordpress page is leaf page

Sometime we want to consider pages which have children as browse pages and may treat them separately in plugins etc. For example we may not want to show related posts in them. Here is the wordpress php code you can use to find if a wordpress page is leaf page and has no child.

function _is_leaf_page($pageid) {
  $args = array(
        'sort_order' => 'asc',
        'sort_column' => 'post_title',
        'hierarchical' => 1,
        'exclude' => '',
        'include' => '',
        'meta_key' => '',
        'meta_value' => '',
        'authors' => '',
        'child_of' => $pageid,
        'parent' => -1,
        'exclude_tree' => '',
        'number' => '',
        'offset' => 0,
        'post_type' => 'page',
        'post_status' => 'publish'
  );
  $pages = get_pages($args);
  return (count($pages) == 0)? True : False;
}

And use the following code to use the above function:

  global $post;
  $post_id = $post->ID;
  $is_leaf = _is_leaf_page($post_id);
Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments