• Hello everyone,

    I’m trying to display breadcrumbs for a page. I’m using get_post_ancestors() function, but it returns an empty array, even though there is definately a parent. Also, I’ve testing it with $post->ancestors which returns an array with page’s parent ID … but get_post_ancestors() still returns an empty array.

    Does anyone know what might be a problem??

    I’m using the following function in my functions.php This is simplified version of code I found in Codex http://codex.wordpress.org/Function_Reference/wp_list_pages#List_current_Page.2C_ancestors_of_current_page.2C_and_children_of_current_page

    <?php
    function breadcrumbs() {
    	global $post;
    	//if the post has a parent
    	if($post->post_parent){
    	  //collect ancestor pages
    	  $relations = get_post_ancestors($post->ID);
    	  //add current post to pages
    	  array_push($relations, $post->ID);
    	  //get comma delimited list of children and parents and self
    	  $relations_string = implode(",",$relations);
    	  //use include to list only the collected pages.
    	  $sidelinks = wp_list_pages("title_li=&echo=0&include=".$relations_string);
    	}
    	if ($sidelinks) { ?>
    	<div id="breadcrumbs">
    		<div class="page-title-inner">
    			<ul>
    				<?php
    				//links in <li> tags
    				echo $sidelinks; ?>
    			</ul>
    		</div>
    	</div>
    	<?php }
    } ?>

    And then I call breadcrumbs() function from my theme templates:

    <?php breadcrumbs(); ?>

    Thanks,
    Dasha

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘get post ancestors function doesn't work?!’ is closed to new replies.