I may be missing what you’re looking for but you never actually echo the cat_ID
in the li and there’s no reason to assign $cat->cat_ID
to $cat_id
.
Additionally, I think you could just as well use something like this to get the category id (or ids) to display the current category(ies) of the current post. (using $category[0]
will give you the first category)
<ul>
<?php
$category = get_the_category();
foreach ( $terms as $term ) { ?>
<li id="tools-<?php echo $category[0]->cat_ID; ?>"><?php echo $term->name; ?></li>
<?php } ?>
</ul>
Related Posts:
- exclude multiple terms using get_terms() function
- Counting number of posts in a category and its sub categories and displaying result using shortcode
- Allow authors to post only in categories they create in WordPress
- Add custom field to attachments in a specific category
- How to Populate the list of custom post type categories
- Php string not working in WordPress Functions.php [duplicate]
- Problems with function on function.php
- Use template for posts with a particular category grandparent
- Check if Product is in a Specific Category in Functions.php
- Woocommerce – remove product from category
- get_the_terms – only top level
- Query all posts of a custom taxonomy term
- get current product name in functions.php
- get post based on category chosen in drop down – The ajax method
- Swapping wp_dropdown_categories function with wp_category_checklist
- Can I prevent the user from adding more than two levels deep of terms inside of a taxonomy metabox?
- Grab posts by multiple categories
- Pass Category Name, Description and Photo into variables to pass to jQuery
- How to pick the default selected value in wordpress dropdown?
- Get page that displays all children of taxonomy parent
- How to show only subcategories in parent category not parent category?
- Dilemma of Populating all the categories in a drop down list
- Edit the_category (); for a hierarchical display
- Displaying posts based on category
- Hierarchical taxonomy list with modificated term links
- Use get() method to grab all categories and output inside another method
- How to get current post category details inside “loop”?
- Show all tags within a category?
- Post not populating for custom post type based on category selection
- Listing titles and custom fields of entries within subcategories
- WordPress custom taxonomy
- making php value numeric
- Taxonomy term description with style, only if the description exists?
- How to change the author box selection on a post to a custom taxonomy
- How to hide a child category and show his parent category
- Dropdown category field inside repeatable metabox
- Display Visual Composer shortcode if a post belongs in specific categories
- WP custom posts: filter results to custom taxonomy tag that corresponds to user meta field
- Get parent category name when only child category is applied to a product
- How to move a post from one category to another
- Notice: Undefined variable
- Hide a specific category title from displaying on site
- Populate Posts based on category selected using AJAX that means without page refresh
- All categories options or All categories not Populating
- Customizing the output of the archive and category widget without altering the original behavior of the widget
- PHP get_category() function redeclared
- str_replace not preserving whitespace
- Custom Taxonomy as Link
- Limit Taxonomy Parents
- Saving category as insert post
- Trying to see if page is category or single and displaying title with appropriate heading tag
- Displaying categories
- How to insert the first letter in uppercase [closed]
- How do I change the Go To Categories link in the term_updated_messages
- Display terms on product page with shotrcode
- Hide child of parent categories already hidden with “get_terms_args” – Woocommerce
- WordPress different custom tag being displayed in on tag list
- Get category name of current post (CPT UI)
- Exclude a category ID from the following function
- Fatal error: Call to undefined function plugin_dir_path()
- Switching wp_get_sites to get_sites
- One button to change all settings in theme customizer?
- Add custom image sizes to media uploader
- Looking to exclude blog posts from category Previous/Next buttons
- Is $hook a global variable in WordPress
- Include a php file from functions.php only to homepage
- Shortcode content output but not in correct place
- Handling Body class based on Template
- Why my filterable portfolio page work not perfectly between slug button and slug output WORDRPESS?
- Automatically adding new post categories to menu
- Get post id in a function when edit/add a post
- get author_name from queried post
- get_template_part based upon post’s category
- How to split links generated into an xml sitemap to avoid exceeding 30 sec maximum execution time?
- Shortcode to find and replace URL
- Function Reference Documenting Template Tags for use in Custom Theme Templates?
- How to hide posts of a specific custom category in WordPress?
- entire JS folder not loading in a WP theme
- Add new post in existing categories using wp_insert_post
- Else/If Statement to Display Photo Descriptions
- Display additional page templates and a sidebar on plugin activation
- I need to display posts in subcategory beside posts in main category
- How to unset a function from a action inside a class, that is called in another class
- Create a new account on site B with data from the purchase of site A
- Why is the current page loaded in the pop-up window and not the specified one?
- Categories does not show Block editor WordPress
- Filtering custom posts by using checkboxes for taxonomy in an ajax loop always gives server 500 error
- Where do I implement this display of User Meta Data, and how to put it in a table?
- Add ID to output only for first option
- Issue encountered while trying to keep website private
- How to make 2 similar functions more efficient
- Custom meta box save values issues – Deprecated: Function ereg() and more
- Insert wordpress tags below posts via functions.php
- Remove the_content From Loop
- My title is showing after the shortcode
- Load a Header in wordpress
- Trouble figuring out how to get my button to submit comment
- Adding multiple conditional tags in a function?
- Query posts by custom taxonomy slug in WP REST API?
- /page/2/ redirect to 404