Getting slug from listing category

Hello !

I am trying to customize the listing categories block, to provide direct links to this category for vendors and services like so :

So I finally figured out that I can copy / paste then edit the listing-category-description.php (my-child-theme/hivepress/listing-category/view/listing-category-description.php). Yes, I am slowly getting the hang of it.

But I need to retrieve the slug of the category to create the links.
At the moment, I am able to retrieve the category name, id :

$listing_category->get_name(); 
$listing_category->get_id(); 

But I would like to retrieve in the case of “Music & Audio”, something like “music-audio” (well, the slug), is there a way to do so ?

I tried debugging with print_r($listing_category); but the slug is not showing.

In “native” WordPress, it would be easy :

$slug = get_post_field( 'post_name', get_post() );

But for some reasons, I can’t manage to get this for the moment…

Cheers !

Problem solved. Sharing is caring, so here’s my solution :

So first I hardcoded the matching slug according to the cat_ID .

function cat_slug($cat_ID){
	switch($cat_ID){
		case 46:
			return 'design-graphisme'; 
		case 47:
			return 'digital-marketing'; 
        //and so on. 
		
		default:
			return 'else'; 
	}	
}

Not really pretty, since it’s hard-coded.

Eventually I found out a better solution :

function get_slug_from_cat_ID($catID){
	// The taxonomy name associated with the custom post type 'hp_listing'
	$taxonomy = 'hp_listing_category'; 

	$term = get_term($catID, $taxonomy);
	return $term->slug; 	
}
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.