![]() |
Hi, I have this shortcode:
When used, I can output a list of taxonomy terms based on a specific taxonomy for a specific post. As an example, I have a custom taxonomy called "Payout Options" with terms like "PayPal", "Payoneer", and "Gift cards". The shortcode would output something like this: "PayPal, Gift cards, Payoneer" (if those terms were selected for a post) This is where I'm having problems: The "Gift cards" term has child terms like "Amazon gift cards" and "Walmart gift cards" If the Gift cards term exists on a post along with "Amazon gift cards" and "Walmart gift cards", I want the shortcode to only output "Gift cards" ignoring the child terms. Is this possible? How would I do this? I would also settle for being able to output like this: "Gift cards (Amazon gift cards, Walmart gift cards), PayPal, Payoneer" because at the moment it outputs in alphabetical order and looks very weird. Hope this makes sense! Thank you. Solution - 1Yry using the get_terms () function instead and use the list of terms as you wish. this is the documentation: https://developer.wordpress.org/reference/functions/get_terms/ If parent => 0 is passed, only top-level terms will be returned
Solution - 2You can use the get_terms function in WordPress with "parent"=>0 attribute, it will return the term object then you can manipulate it with a for loop to get the desired output. https://developer.wordpress.org/reference/functions/get_terms/ Term Object that is returned will look something like this WP_Term Object ( [term_id] => [name] => [slug] => [term_group] => [term_taxonomy_id] => [taxonomy] => [description] => [parent] => [count] => [filter] => ) You can loop through the terms to get the desired output as you desire Thanks Monit |
Wordpress |
Type: | Code Example |
Category: | Coding |
Sub Category: | Code Example |
Uploaded by: | Admin |
Views: | 27 |