Skip Navigation

[Resolved] Implementing Category Slugs as Classes

This support ticket is created 6 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 1 reply, has 2 voices.

Last updated by Luo Yang 6 years, 1 month ago.

Assisted by: Luo Yang.

Author
Posts
#1181772

I've done this quite a few times before with no issues but for some reason, I'm finding myself baffled with this particular project. It seems around every corner, there's some sort of conflict that doesn't support my usual Toolset usage. I'm not sure if it's just some new abstract conflict because of Gutenberg themes, or what. But anyway...

The outline:
Custom Post Type: Project Gallery
Custom Fields: Rendered as a shortcode
PHP Template: template-projects.php (displays project gallery)
PHP Template: single-project-gallery (displays individual projects and their details)

The issue:
I am trying to call the CPT's categories (there are four of them) and render their slugs to implement them as classes to use as a filter.
So:
`<div class="col-md-4 project-gallery filter-<?php echo $categories(); ?> project-<?php echo $projectId; ?>">`

So on the front end it will look like:

`<div class="col-md-4 project-gallery filter-all filter-pedestrial-bridges project-483">`

For some reason, it absolutely will not render the categories. I've combed through Toolset forums, Toolset documentation and instructional posts, Stackoverflow, and a million other resources trying snippet after snippet and NOTHING is working. I'm by no means a PHP wizard, but I can generally make things work and for the most part I know what I'm doing... until now.

I'd so, SO appreciate any direction you could point me in because I'm pulling my hair out at this point.

#1182062

Hello,

It seems you are going to use PHP codes to display term's slugs.

I suggest you try wordpress function get_the_terms();
https://developer.wordpress.org/reference/functions/get_the_terms/
Retrieve the terms of the taxonomy that are attached to the post.

For example:

$terms = get_the_terms( $projectId, 'category' );
$arr= array();
foreach($terms as $term){
$arr[] = $term->slug;
}
echo $slugs =  implode(', ' $arr);

For your reference.