How to use Custom Term (Advanced) to show all tags associated with a post not ju

#556448
  • Resolved Anonymous
    Rank Math free

    How to use Custom Term (Advanced) to show all tags associated with a post not just one this %customterm(artist)% show only only one tag the first one

Viewing 5 replies - 1 through 5 (of 5 total)
  • Anonymous
    Rank Math free

    And how to hide if the is no taxonomy associated with a post

    Nigel
    Rank Math business

    Hello,

    Thank you for contacting Rank Math for help with adding a list of all your custom terms as a variable in the SEO meta of your post.

    Rank Math does not have a variable to display the list of terms as a string. You will have to create a custom variable to display list of terms. I have created the custom variable ‘%cat_terms%’ to do it. Please add the following code snippet to your website and replace %customterm(artist)% with %cat_terms%.

    
    /**
     * Action: 'rank_math/vars/register_extra_replacements' - Allows adding extra variables.
     * Snippet to register variable that will return the number of posts in the current term
     */
    add_action('rank_math/vars/register_extra_replacements', function () {
        rank_math_register_var_replacement(
            'cat_terms',
            [
                'name'        => esc_html__('Category Terms', 'rank-math'),
                'description' => esc_html__('Number of posts in the current term', 'rank-math'),
                'variable'    => 'cat_terms',
                'example'     => cat_terms_callback(),
            ],
            'cat_terms_callback'
        );
    });
    function cat_terms_callback()
    {
    	global $post;
    	if ( !has_term('', 'category')) return; //replace 'category' with your custom taxonomy name.
    	$post_id = $post->ID;
        $terms_list_objects = get_the_terms( $post_id, 'category'); //replace 'category' with your custom taxonomy name.
    	$terms_list_array = array_map(function ($object) { return $object->name; }, $terms_list_objects);
    	$terms_list_string = implode(', ', $terms_list_array);// replace ', ' with your preferred spacer
    	return $terms_list_string;
    }
    

    In the code snippet, find the lines to replace ‘category’ with ‘artist’. You can follow this guide for how to add a code snippet to your website: https://rankmath.com/kb/wordpress-hooks-actions-filters/

    Hope that helps. Please let us know if you have questions.

    Anonymous
    Rank Math free

    Thank you very much it works but one more thing, for spacer if there is more than Five Tags it will be like this “Song name By Artist 1, Artist 2, Artist 3, Artist 4, Artist 5” I want to be able to add FT. “Featuring” so if i add “FT.” to spacer and will look like this “Song name By Artist 1 FT. Artist 2 FT. Artist 3 FT. Artist 4 FT. Artist 5”

    I will like it do look like this “Song name By Artist 1 FT. Artist 2, Artist 3, Artist 4 & Artist 5” is this possible.?

    Anonymous
    Rank Math free

    ChatGPT helped with this and it works Check it if its oky

    /**
     * Action: 'rank_math/vars/register_extra_replacements' - Allows adding extra variables.
     * Snippet to register variable that will return the number of posts in the current term
     */
    add_action('rank_math/vars/register_extra_replacements', function () {
        rank_math_register_var_replacement(
            'cat_terms',
            [
                'name'        => esc_html__('Artist Terms', 'rank-math'),
                'description' => esc_html__('List of artist terms in the current post', 'rank-math'),
                'variable'    => 'cat_terms',
                'example'     => cat_terms_callback(),
            ],
            'cat_terms_callback'
        );
    });
    
    function cat_terms_callback()
    {
    	global $post;
    	if (!has_term('', 'artist', $post)) {
            return;
        }
    	$post_id = $post->ID;
        $terms_list_objects = get_the_terms($post_id, 'artist');
    	$terms_list_array = array_map(function ($object) { return $object->name; }, $terms_list_objects);
    
        $terms_count = count($terms_list_array);
        if ($terms_count == 0) {
            return;
        } elseif ($terms_count == 1) {
            $terms_list_string = $terms_list_array[0];
        } else {
            $last_term = array_pop($terms_list_array);
            if ($terms_count > 3) {
                $terms_list_string = implode(', ', $terms_list_array) . ', & ' . $last_term;
            } else {
                $terms_list_string = implode(' ft. ', $terms_list_array) . ' Ft. ' . $last_term;
            }
        }
    
    	return $terms_list_string;
    }

    Hello,

    Thank you for the update.

    We are glad that you have already managed to achieve displaying the tags and appreciate it for sharing the filter that works on your site.

    If you have any other concerns, please don’t hesitate to contact us anytime to assist you further.

    Looking forward to helping you.

    Thank you.

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

Viewing 5 replies - 1 through 5 (of 5 total)

The ticket ‘How to use Custom Term (Advanced) to show all tags associated with a post not ju’ is closed to new replies.