Horje
How to show only parent terms with get_the_term_list() shortcode - Wordpress Solution
Hi, I have this shortcode:
function post_taxonomy_sc( $atts ) {
    $defaults = [
        'post_id' => get_the_id(),
		'taxonomy' => 'category',
        'before' => '',
        'sep' => ', ',
        'after' => '',
        'linked' => 'yes',
    ];

	$atts = shortcode_atts($defaults, $atts, 'post_taxonomy');

    if($atts['linked'] == 'yes'){
        $terms = get_the_term_list($atts['post_id'], $atts['taxonomy'], $atts['before'], $atts['sep'], $atts['after']);
        } 
    else {
        $terms = strip_tags(get_the_term_list($atts['post_id'], $atts['taxonomy'], $atts['before'], $atts['sep'], $atts['after'] ));
        }
    return "<span class=\"post-taxonomy-terms-sc\">" . $terms . "</span>";
}
add_shortcode( 'post_taxonomy', 'post_taxonomy_sc' );
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 - 1

Yry 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

$terms = get_terms( array( 
    'taxonomy' => 'tax_name',
    'parent'   => 0
) );


Solution - 2

You 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/


$terms= get_terms( array(
    		'taxonomy' => 'category',
    		'parent' => 0
			) );
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] => )

foreach ($terms as $term) {
					echo '<a rel="nofollow" href="'.get_category_link($term->ID).'">'.$term->name.'</a>, ';
				}
You can loop through the terms to get the desired output as you desire Thanks Monit





Wordpress

Related
woocommerce order splitting - Wordpress Solution woocommerce order splitting - Wordpress Solution
Display post loop associated with current category url slug - Wordpress Solution Display post loop associated with current category url slug - Wordpress Solution
L.S. I'am developing an website for a local photoclub, i using Nextgen - Wordpress Solution L.S. I'am developing an website for a local photoclub, i using Nextgen - Wordpress Solution
wp query with multiple of same meta keys - Wordpress Solution wp query with multiple of same meta keys - Wordpress Solution
list of the children of a Woocommerce Grouped product ( with the Product picture,name and price ) - Wordpress Solution list of the children of a Woocommerce Grouped product ( with the Product picture,name and price ) - Wordpress Solution

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
27