Nathan Hangen
Nathan Hangen

Reputation: 73

Converting WordPress Category ID to Category Name

So, I'm working on a WordPress theme that uses a custom taxonomy to create a handy web form.

Right now it prints Grandparent and child, but I need it to print:

Grandparent -> Parent -> Child

I've been been able to get the ID of the Parent, but cannot print that parent's name, no matter what I try.

Here's what I have to get the id:

$adCategory = get_term_by('id',$_POST['cat'],'ad_cat'); 
$_POST['catname'] = $adCategory->name;
$mainCategory = get_term_by('id',$_POST['main_cat'],'ad_cat');              
$mainCat = $mainCategory->name;
$categoryParent = get_term_by('id',$_POST['cat'],'ad_cat');
$catParents = $categoryParent->parent;

(first group prints child, second prints grandparent, third prints id

and here is how I'm able to print them all on the page:

<?php echo $catParents; ?>
<?php echo $mainCat; ?>    
<?php echo $_POST['catname']; ?>

I've tried get_cat_name, but it won't work, just returns empty. Any ideas?

Upvotes: 2

Views: 685

Answers (1)

Tim Bezhashvyly
Tim Bezhashvyly

Reputation: 9090

If you already have an ID you can at least make a direct query

$wpdb->get_var('SELECT name FROM '.$wpdb->terms.' WHERE term_id = '.$term_ID);

Upvotes: 1

Related Questions