Returning Category Parent ID (1 post)

  1. GrahamDoel
    Posted 9 years ago #

    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: http://wordpress.org/support/topic/95256

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


Topic Closed

This topic has been closed to new replies.

About this Topic