Posts in WordPress are hierarchical and can have ancestors (Parents). If you have posted on your blog with ancestors, you might want to display a list of ancestors of the posts. There’s an easy way to do it.

Use this code snippet inside your theme’s single.php file:

global $post; // Get the current post.

$ancestors = get_post_ancestors($post); // Get all the ancestors of this post.

You can then iterate over this array of ancestors to output a list of the ancestors like this:

global $post; // Get the current post.

$ancestors = get_post_ancestors($post); // Get all the ancestors of this post.

foreach ($ancestors as $ancestor){
	echo "<a href='" . get_the_permalink($ancestor) . "'>" . get_the_title($ancestor) . "</a>";
}

The above code will output links to all the ancestors of the current post.

Pin It on Pinterest

Share This