0

I'll start by saying that I have no background in php.

I need to embed a shortcode to display the name of the specific taxonomy the post is associated with.

The shortcode is embedded in a single post template.

Remarks:

  1. I don't need a link to the taxonomy.
  2. how can i embedded it as a shortcode?

I would appreciate your help

1
  • developer.wordpress.org/reference/functions/add_shortcode: "Every shortcode callback is passed three parameters by default, including an array of attributes ($atts), the shortcode content or null if not set ($content), and finally the shortcode tag itself ($shortcode_tag), in that order." - you are currently invoking the get_the_terms directly, with those mentioned parameters - which make no sense whatsoever, for that function. You need to wrap your own code into a custom function, and then specify that function as the callback function. Commented Jun 6, 2024 at 9:42

2 Answers 2

0

If you want show only one taxonomy you can try this following version of code.

function display_post_single_tax_terms( $atts ) {
    // Get post ID
    $post_id = get_the_ID();

    // Check if post ID exists
    if ( $post_id ) {
        // Get taxonomy terms associated with the post
        $terms = get_the_terms( $post_id, 'your_taxonomy_name' ); // Replace 'your_taxonomy_name' with the name of your taxonomy

        // Check if terms exist
        if ( $terms && ! is_wp_error( $terms ) ) {
            return $terms[0]->name; // you can wrap this with any tag if you want;
        }
    }

    return ''; // Return empty string if no taxonomy terms found
}
add_shortcode( 'display_post_taxonomy', 'display_post_single_tax_terms' );
Sign up to request clarification or add additional context in comments.

Comments

0

Please try the following code to fetch all the taxonomies associated with post.

make sure you're receiving the proper post ID and correct taxonomy name, so you may need a little bit of debugging, please let me know if you're facing any issues. I'll be happy to help further.

function display_post_taxonomy_terms( $atts ) {
    // Get post ID
    $post_id = get_the_ID();

    // Check if post ID exists
    if ( $post_id ) {
        // Get taxonomy terms associated with the post
        $terms = get_the_terms( $post_id, 'your_taxonomy_name' ); // Replace 'your_taxonomy_name' with the name of your taxonomy

        // Check if terms exist
        if ( $terms && ! is_wp_error( $terms ) ) {
            $taxonomy_list = '<ul>';
            foreach ( $terms as $term ) {
                $taxonomy_list .= '<li><a href="' . esc_url( get_term_link( $term ) ) . '">' . esc_html( $term->name ) . '</a></li>';
            }
            $taxonomy_list .= '</ul>';

            return $taxonomy_list;
        }
    }

    return ''; // Return empty string if no taxonomy terms found
}
add_shortcode( 'display_post_taxonomy', 'display_post_taxonomy_terms' );

3 Comments

Well, do you want to get only one category name without the link?
Thank you very much! This seems to give me the required answer, except for minor changes I had to make to the code. In order for the name of the taxonomy to be displayed without a link and not in a list view, I made the changes I attached in the answer below. It seems to work as it should, I hope I deleted only what needed to be deleted and nothing more than that, and also that I didn't leave anything unnecessary in the code following the deletion.
Yes, only for one category. Because in the case of this specific taxonomy I only assigned each post to one category (taxonomy).

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.