Get the top parent page in WordPress

Share/Save

A couple of days ago I found myself with the issue of getting the top parent of any page (that one which doesn’t have any father), so after look in WordPress documentation I found a nice function called get_page_hierarchy() and also no documentation about it. Without documentation about the function it was going to be a little bit tricky to know wheter or not this was what I was looking for, so I decided create my own function to receive the post_name of the top parent page. As soon I started I realized that my knowledges about recursivity were a little bit rusty, but I got it … :)

Here is the code:

<?php
//return the name of the top parent page base on a page_id
function getTopParentPostName($myid){
	$mypage = get_page($myid);

	if ($mypage->post_parent == 0){
		return $mypage->post_name;
	}
	else{
		return getTopParentPostName($mypage->post_parent);
	}

}?>
Share/Save

14 Responses to “Get the top parent page in WordPress”

  1. I like people notice all the details and minutiae of everyday little thing could find something attractive and imperceptible to most. Super!

  2. Thanks for the code! Definitely saved me some headaches, just as I was starting to get into this same problem.

  3. Nice work. was looking for exactly how to do this. I tweaked it to “return $mypage” so i could get at everything, which is really helpful.

    nice looking site too.

    Thanks!

  4. You are the man aren’t you?!! Just what I was looking for, thank you for saving me about 21 minutes.

  5. [...] Now comes the fun part. Let’s get those sidebars showing up when you want them to. You’re going to want to add this code to your functions.php file. I won’t explain what it does until the next part (trust me) (also, thanks to Alberto for this post: http://www.altrugon.com/php/get-the-top-parent-page-in-wordpress/) [...]

  6. Thanks for a wonderfully simple and easy to implement function. Worked like a charm!

  7. Thank you for saving me… : – ) .. Im a so happy now cuz my website project is almost done.. thank you!!!!

  8. Hey, thanks for sharing this. I came across the same function and couldn’t figure out how it worked.

  9. Thanks so much! This is very helpful!

  10. dude, seriously… You rule! :) This saved me a lot of hassle with getting the very upper parent of a certain page.

    THANKS!

  11. Hola, gracias por el código, tengo una pregunta tengo n problema parecido pero no lo he podido resolver. resulta que tengo unas paginas_padres->subpaginas->paginas; en estas últimas necesito meter un codigo por medio de get_template_part(‘micodigo’);
    El problema está en que necesito que se haga automaticamente por medio de un condicional que me diga ” si la página es hija de la subpagina =incluya el código.

    no lo puedo hacer por:

    pues me tocaría incluir el codigo cada vez que se incluya alguna otra página.si hay alguna idea te lo agradezco.

  12. Jorge me encantaria ayudarte pero no entiendo muy bien tu problema y tambien hace mucho tiempo que no programo en wordpress. Si me puedes aclarar mejor el problema tal vez pueda echarte una mano.

  13. Hi, I realize this is an old post, but could you help me place this name into a title? I’m assuming you put this code into the functions.php file? What do you do to call it in? I’d like to have something like: Top Parent

    Thank you!

  14. Really Nice, It worked for me .Thanks

Leave a Reply

* Required fields.