0

Taxonomy name is "intro". I'm inside a shortcode so I dont need to echo.I need a variable for the selected taxonomy term to put in the return. What I do has no effect

$terms2 = wp_get_post_terms($post->ID,'intro',array('fields' => 'names'));

2 Answers 2

0

This is the solution:

$term_name = get_the_terms(get_the_ID(), 'intro')[0]->name;
0

If you want to retrieve the selected term of a custom taxonomy called "intro" for a particular post inside a shortcode, you can use the wp_get_post_terms() function to fetch the terms associated with that post

function my_custom_shortcode_function() {
    global $post;
    
    // Get the terms for the "intro" taxonomy associated with the current post
    $terms = wp_get_post_terms($post->ID, 'intro', array('fields' => 'names'));

    // Check if any terms are retrieved
    if (!empty($terms)) {
        // Assuming you want to use the first term only
        $selected_term = $terms[0];
    } else {
        // If no terms are retrieved, set a default value or handle the case as needed
        $selected_term = 'No Term Selected';
    }

    // Now you can use $selected_term as needed
    return $selected_term;
}
add_shortcode('my_shortcode', 'my_custom_shortcode_function');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.