Obtener la página top padre en WordPress

Share/Save

Hace un par de días me encontre con el problema de que tenía que conseguir la paginá top padre (aquella página que no tiene ningún padre), así que después de mirar en la documentación de WordPress encontré una función llamada get_page_hierarchy() y ninguna información sobre ella. Sin documentación iba a ser un poquito difícil saber si era lo que estaba buscando o no, así que decidí crear my propia funcion para recibir el post_name del la página top padre. Tan pronto como empecé me di cuenta de que mis conocimientos sobre recursividad estaban algo oxidados, pero lo conseguí … :)

Aquí va el código:

<?php
//devuelve el nombre del top padre de la página que le especificamos mediante el 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

8 Responses to “Obtener la página top padre en 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/es/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.

Leave a Reply

* Required fields.