jeudi 2 février 2017

Function to find wordpress custom post type ancestor

I have been struggling a lot recently with a CPT I'm working with in wordpress.

I have a cpt setup called cats and rewrote the url to 'rewrite' => array( 'slug' => 'our-animals/rehome-a-cat', 'with_front' => false) so the structure is our-animals > rehome-a-cat > cats (individual cpt page).

I want to show a menu on each of those pages which will show the ancestor (our-animals) and the any children minus each indivdual cpt. so for example if I had added a rehome-a-dog page and dogs cpt the menu for our-animals, rehome-a-dog, rehome-a-cat and each indivdual cat and dog cpt will show, our-anmals, rehome-a-dog and rehome-a-cat.

Currently I have a fucntion the find teh ancestor of a page and then displays the menu as I want:

function my_get_ancestor_id($post) {
    if ( $post->post_parent ) {
        $ancestors = get_post_ancestors($post->ID);
        $root = count($ancestors)-1;
        $parent = get_post($ancestors[$root]);
    } else {
        $parent = $post;
    }
    return $parent->ID;
}

and a function to generate my menu

if ( ! function_exists( 'wpb_list_child_pages' ) ) {    
  function wpb_list_child_pages() {
    global $post;   
    $parent_id = my_get_ancestor_id($post);
    if ( is_page() && $post->post_parent ){
      $args = array(
        'sort_order' => 'ASC',
        'sort_column' => 'menu_order',
        'hierarchical' => 1,
        'exclude' => '',
        'child_of' => $parent_id,
        'parent' => -1,
        'exclude_tree' => '',
        'number' => '',
        'offset' => 0,
        'post_type' => 'page',
        'post_status' => 'publish'
      ); 
      $childpages = get_pages($args); 
    }else{
      $args = array(
        'sort_order' => 'ASC',
        'sort_column' => 'menu_order',
        'hierarchical' => 1,
        'exclude' => '',
        'child_of' => $parent_id,
        'parent' => -1,
        'exclude_tree' => '',
        'number' => '',
        'offset' => 0,
        'post_type' => 'page',
        'post_status' => 'publish'
      ); 
      $childpages = get_pages($args); 
    }

    if ( $childpages ) {
        $string = '<ul class="hh-rightmenu-ul">';
        foreach($childpages as $page){
         $string .= '<a href="'.get_page_link( $page->ID ).'"><li class="hh-rightmenu-li"><div class="hh-rightmenu-li-inner">'.$page->post_title.'</div></li></a>';
        }
        $string .= '</ul>';
    }
    echo $string; 
  }

}

Now on our-animals and rehome-a-cat it works great, but I think because teh cpt cats doesnt have a actual physcial parent or something the my_get_ancestor_id function doesnt work when you go to each CPT and no menu is displayed. The second condition in my menu function shows controls the menu on posts as if i put 106(our-animals ID) in place of 'child_of' => $parent_id, it shows the menu I want.

So basically I'm struggling to find a way in my ancestors function to maybe get the ancestor from the slug if its a CPT as it can't find any parent ID.

Any thoughts?

Any help appreciated.

thanks in advance.

Ian

Aucun commentaire:

Enregistrer un commentaire