Support » Plugins » Testing and obtaining category_parent ID

  • I haven’t used WordPress for long, and know nowt about php. So I am having to ask for help.
    I am working on this function
    function get_here($current){
    $current = $_GET[cat];
    echo $current;

    Which returns whichever category id you happen to be in. Which is fine when they are not ‘children’. When they are it is causing problems, soI need to amend it to test for the existence of a category_parent(greater than 0 anyway), and return the relevant ID.
    Can anyone help me with this? I have been playing with it all day and got absolutely nowhere.
    All help appreciated…

Viewing 5 replies - 1 through 5 (of 5 total)
  • get_the_category() returns the category of the current post.
    get_the_category_parents(get_the_category()) will return the parent of the current category.
    Take a look at wp-includes/template-functions-category.php for more options.

    I don’t seem to have the get_the_category_parents function (v1.2), but anyway to offer a little help, this is the raw material of a solution, it lists each category and it’s parent status.
    $catp = $wpdb->get_results("SELECT cat_ID, category_parent FROM $tablecategories");
    foreach($catp as $tcatp) {
    $tid = $tcatp->cat_ID;
    $tpid = $tcatp->category_parent;
    print "id $tid p $tpid<br>";


    hmm from what I have tried this doesn’t work, I shall be calling the function from within the <head> which might explain my difficulty.
    So is it me being dumb?
    I have been through that file, but didn’t find what I was looking for.
    Am I missing something painfully obvious here?

    hmm, thanks Andrew I’ll have a look at using that.
    Steep learning curve here, so maybe I should say ‘attempt’ to utilise it.


    I was searching for something like this too and I wrote my own function because I didn’t found anything useful 🙁 or didn’t knew how to use the “get_the_category_parents()” function…

    So here it is:
    function get_parent_category($id)
    global $wpdb;

    $category = $wpdb->get_results("SELECT cat_ID, cat_name, category_nicename, category_description, category_parent FROM ".$wpdb->categories." WHERE cat_ID=".$id);

    if( isset($category) && !empty($category) ) :
    $category_output = array();
    $category_output['cat_ID'] = $category[0]->cat_ID;
    $category_output['cat_name'] = $category[0]->cat_name;
    $category_output['category_nicename'] = $category[0]->category_nicename;
    $category_output['category_description'] = $category[0]->category_description;
    $category_output['category_parent'] = $category[0]->category_parent;
    return $category_output;
    else :
    return FALSE;

    You will use it like this for example:
    if( $cat > 0 ) :
    $this_category = get_parent_category($cat);
    <h2><?php echo $this_category['cat_name']; ?></h2>
    <?php echo $this_category['category_description']; ?>
    <?php endif; ?>

    Hope you’ll understand. You will get “cat_ID”, “cat_name”, “category_nicename”, “category_description” and “category_parent” in the variable that you assign the results of the “get_parent_category()” function.
    “get_parent_category($cat)” is the call of the function, where “$cat” is the current category_ID and is usually direct available.

    Hope this works. Maybe you’ll have to change it a bit, this is what I needed for my page.

    Cheers, Boby!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Testing and obtaining category_parent ID’ is closed to new replies.