Obtener la página top padre en WordPress
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);
}
}?>
Home
noviembre 5th, 2008 at 22:12
I like people notice all the details and minutiae of everyday little thing could find something attractive and imperceptible to most. Super!
febrero 13th, 2009 at 13:58
Thanks for the code! Definitely saved me some headaches, just as I was starting to get into this same problem.
febrero 17th, 2009 at 10:35
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!
abril 17th, 2009 at 06:43
You are the man aren’t you?!! Just what I was looking for, thank you for saving me about 21 minutes.
abril 30th, 2009 at 19:28
[...] 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/) [...]
enero 4th, 2010 at 12:37
Thanks for a wonderfully simple and easy to implement function. Worked like a charm!
febrero 25th, 2010 at 19:02
Thank you for saving me… : – ) .. Im a so happy now cuz my website project is almost done.. thank you!!!!
marzo 8th, 2010 at 05:48
Hey, thanks for sharing this. I came across the same function and couldn’t figure out how it worked.
agosto 6th, 2010 at 23:50
Thanks so much! This is very helpful!
noviembre 8th, 2010 at 08:12
dude, seriously… You rule!
This saved me a lot of hassle with getting the very upper parent of a certain page.
THANKS!
diciembre 2nd, 2010 at 14:52
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.
diciembre 2nd, 2010 at 19:39
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.
abril 13th, 2011 at 08:33
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!
noviembre 17th, 2011 at 03:38
Really Nice, It worked for me .Thanks