Use of translated keywords in schemas

#840128
  • Resolved Alireza
    Rank Math free

    Hi! I use translatepress to translate my website into different languages. Currently, I use filters in my functions.php to insert keywords in my category and product pages schema. Can you guide me on how to dynamically insert translated keywords in different product and category pages? Here’s a filter I use for my category pages.

    // add collectionpage schema to product category pages
    add_filter( 'rank_math/json_ld', function( $data, $jsonld ) {
        if( is_product_category() ) {
            $current_category = get_queried_object();
            $category_name = single_term_title('', false);
            
            $products = get_posts( array(
                'post_type' => 'product',
                'numberposts' => -1,
                'post_status' => 'publish',
                'fields' => 'ids',
                'tax_query' => array(
                    array(
                        'taxonomy' => 'product_cat',
                        'field' => 'term_id',
                        'terms' => $current_category->term_id,
                    )
                ),
            ));
            
            // Get the category description
            $category_description = term_description( $current_category->term_id, 'product_cat' );
            $category_description = wp_strip_all_tags( $category_description );
            
            // If description is empty, use a default
            if ( empty( $category_description ) ) {
                $category_description = "Explore our collection of " . $category_name;
            }
            
            // CollectionPage schema
            $collectionPage = array(
                '@type' => 'CollectionPage',
                'name' => $category_name . ' Collection',
                'description' => $category_description,
                'url' => get_term_link( $current_category ),
                'numberOfItems' => count( $products ),
                'genre' => $category_name,
                'keywords' => $category_name,
                'breadcrumb' => array(
                    '@type' => 'BreadcrumbList',
                    'itemListElement' => array(
                        array(
                            '@type' => 'ListItem',
                            'position' => 1,
                            'item' => array(
                                '@id' => home_url(),
                                'name' => 'Home'
                            )
                        ),
                        array(
                            '@type' => 'ListItem',
                            'position' => 2,
                            'item' => array(
                                '@id' => get_permalink( wc_get_page_id( 'shop' ) ),
                                'name' => 'Shop'
                            )
                        ),
                        array(
                            '@type' => 'ListItem',
                            'position' => 3,
                            'item' => array(
                                '@id' => get_term_link( $current_category ),
                                'name' => $category_name
                            )
                        )
                    )
                )
            );
            
            // Replace the existing data with our CollectionPage schema
            $data = array(
                '@context' => 'https://schema.org',
                '@graph' => array( $collectionPage )
            );
        }
    	
        return $data;
    }, 99, 2 );
Viewing 3 replies - 1 through 3 (of 3 total)
  • Hello,

    Thank you for contacting Rank Math support.

    The filter you have shared is for adding the CollectionPage Schema. It doesn’t add keywords to the Schema. Please share the correct filter so we can assist you further.

    Looking forward to helping you.

    Alireza
    Rank Math free

    Hi! The filter does add keywords as you can see with the line 'keywords' => $category_name,. I’m not sure why you say the filter doesn’t add keywords.

    Hello,

    The code is already populating the keyword key with the title of the taxonomy where the filter is running. Assuming that you have translated the titles of the taxonomies, this WordPress function will fetch the data in the appropriate language of the taxonomy being loaded.

    Regarding the product pages, you cannot add the keywords key to those because it’s not included in the Product Schema guidelines.

    Last but not least, please note that extensive custom coding is outside the scope of Rank Math support. Our team does try to help with some small snippets to help people expand the behavior of the plugin but beyond small snippets, we don’t provide custom implementations.

    Don’t hesitate to get in touch if you have any other questions.

    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 3 replies - 1 through 3 (of 3 total)

The ticket ‘Use of translated keywords in schemas’ is closed to new replies.