wordpress get_parent function

Thursday, August 14th, 2008 @ 11:33 am | php, wordpress

 I was looking for a way to get the parent ID of the current subpage recently and could not find an available function on the codex pages. I needed this function in order to create a navigation that is always based on the current page. The site I was working on has no more than 3 levels of navigation, so I needed a way for the deepest sub-pages to generate the same side navigation as the 2nd level pages. I wrote this short simple function.

Hopefully someone may find it usefull.

//Gets the parent ID of a post or page
function get_parent($parent) {
global $wp_query;
$return = $wp_query->post->post_parent;

return $return;
}

 

Recently

  • I will never buy an Adobe product ever again!
  • XNA Tutorial: HLSL and SpriteBatch for 2D effects.
  • Will Philly make me cry again this week
  • Photoshop takes a long time to open or create files
  • Fight Monkey of Magic
  • David Stern must read my blog
  • The most over hyped non-storm of all time
  • cforms ii custom submit script
  • wordpress get_parent function
  • Flexible Navigation – Wordpress Plugin *beta
  •  

    6 Responses to “wordpress get_parent function”

    1. Vinicius Massuchetto Says:

      Quite useful, I might say. :)
      Thanks

    2. Noel Nuguid Says:

      Hi Sir,

      I was thinking of using your code as breadcrumbs for my pages.

      but since im a Newbie wordpress coder here. Im pretty unsure on how do i implement this? do i just place your code in the functions.php?

      how do i call it from the theme?

    3. Angélica Says:

      Thanks a lot, very useful to me.

    4. Cristian Says:

      Thank you so much for this simple function. Works like a charm

      @Noel yes, drop this in your functions.php file. Then call it.

      Cris

    5. Corentin Says:

      Where i have to insert this code ? (In wich file)
      Thanks

    6. MarkW Says:

      Nice Code. Helped me quite a bit. thanks

    Leave a Reply

    XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">