Support » Themes and Templates » Returning Category Parent ID

  • I would like to return the category parent ID for use in my theme. So far the solution I have come up with is

    1. Enter the follwoing in my themes functions.php

    function get_category_parent_id($id) {
    $parent = &get_category($id);
    $name = $parent->cat_ID;
    if ( $parent->category_parent )
    $parent = &get_category($parent->category_parent);
    $name = $parent->cat_ID;
    return $name;

    2. Enter the following in the theme template where you want the id to apprear:

    <?php foreach((get_the_category()) as $cat){ echo(get_category_parent_id(get_category($cat)))} ?>

    I borrowed the idea from:

    All this seems a little bit long winded. Am I missing something? Is there a simpler solution?


  • The topic ‘Returning Category Parent ID’ is closed to new replies.