I have a custom post type called 'listing' and a custom taxonomy called 'listing-category'
I am trying to create a class for the single-listing.php posts that is the slug of the parent custom taxonomy only eg:
STAY - Hotel - B&B
EAT - Cafes - Bakery
SHOP - Food - Shoes
So for single posts under hotel - I need to create a class of 'stay' as all posts under that class will have the same style.
What I hope to output is for example:
<div id="main-content" class="stay">
This previous question seems closest but I can't seem to get it working for my project: https://wordpress.stackexchange.com/questions/24794/get-the-the-top-level-parent-of-a-custom-taxonomy-term
I've been going around in circles for about two days with this so any help would be really appreciated!
Actually that answer you have given the link to is OK and solves your problem as well. May be you do wrong implementation.
Here is sample code for your case:
global $post;
$ctp_listing_cats=wp_get_post_terms( $post->ID, 'listing-category' );
if (!empty($ctp_listing_cats[0]->term_id)){
$topparent=get_term_top_most_parent($ctp_listing_cats[0]->term_id,'listing-category');
}
//...
echo '<div id="main-content" class="'.$topparent->name.'">';
get_term_top_most_parent()
function of the given code is the function from another answer you gave link to.