/    Sign up×
Community /Pin to ProfileBookmark

How to Get current post term parent by child ID

I have ID of term child, I need find out this child-parent by child ID, it’s possible, maybe someone can help with this?

I used this but its not working
$terms = get_the_terms( $_product->id , ‘product_cat’);
if($terms) {
foreach( $terms as $term ) {
$term = get_term_by(“id”, $term->parent, “product_cat”);
if ($term->parent > 0) {
$term = get_term_by(“id”, $term->parent, “product_cat”);
}
$cat_obj = get_the_terms($term->term_id, ‘product_cat’);
$cat_name = $cat_obj->name;
}
}
echo ‘<br />(‘. $cat_name . ‘)’;

to post a comment
HTML

2 Comments(s)

Copy linkTweet thisAlerts:
@jedaisoulDec 12.2015 — Hi and welcome. Is your code PHP? It certainly isn't html. Please clarify and I will move the thread a appropriate.
Copy linkTweet thisAlerts:
@kite123Dec 31.2015 — hey,

The get_term function is used here:

$term_id = 21; // Lucky number ?

$child_term = get_term( $term_id, 'category' );

$parent_term = get_term( $child_term->parent, 'category' );

Note:-

Replace 'category' with whatever taxonomy you're using.
×

Success!

Help @msxpert spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...