-
hi, i have 2 problem…
1- How to change the “schema title” of the product?
I want %sitename% not to be in the product schema headline, be only %title%, I found it for the post, but I don’t know where it is for the product.2- I want to change the “title” of the products belonging to a certain category and add a word to the beginning of it, how?
word: photo of
-
Do I have to replace the old code with these new codes? Or should I put it after that?
Your new codes could not be saved and gave a syntax error.
I rewrote the new codes with the help of chatgpt, and used them along with the previous ones you gave me.
The result was what you see below:add_filter( 'rank_math/frontend/title', function( $title ) { global $post; if ( is_product() ) { $terms = get_the_terms( $post->ID, 'product_cat' ); if ( is_array( $terms ) ) { $term_ids = wp_list_pluck( $terms, 'term_id' ); if ( in_array( '36', $term_ids ) ) { $title = 'Photo of ' . $title; } } } return $title; }); add_filter( 'rank_math/frontend/breadcrumb/items', function( $crumbs, $class ) { global $post; if ( is_product() ) { $terms = get_the_terms( $post->ID, 'product_cat' ); if ( is_array( $terms ) ) { $term_ids = wp_list_pluck( $terms, 'term_id' ); if ( in_array( '36', $term_ids ) ) { $last_crumb_index = count( $crumbs ) - 1; $crumbs[ $last_crumb_index ][0] = 'Photo of ' . get_the_title(); } } } return $crumbs; }, 10, 2 ); add_filter( "rank_math/snippet/rich_snippet_product_entity", function( $entity ) { global $post; $term = get_the_terms( $post->ID, 'product_cat' ); $term_id = wp_list_pluck($term, 'term_id'); if(in_array('36', $term_id)) { $entity['name'] = 'Photo of '.get_the_title(); return $entity; } $entity['name'] = get_the_title(); return $entity; }); add_filter( 'rank_math/json_ld', function( $data, $jsonld ) { global $post; if(is_product()) { $term = get_the_terms( $post->ID, 'product_cat' ); $term_id = wp_list_pluck($term, 'term_id'); if(in_array('36', $term_id)) { $data['WebPage']['name'] = 'Photo of '.get_the_title(); return $data; } $data['WebPage']['name'] = get_the_title(); return $data; } return $data; }, 9999, 2);
This solved my problem completely.
But I have a question, isn’t there an extra code that is useless? Or do the same thing twice? Or will it bring us problems in the future?
Please correct it if there is a problemHello,
Yes you are correct.
To improve the given code for better performance and maintainability in WordPress, we can optimize it by reducing the redundant calls to global variables and database queries.
Here’s a refined version of the code:
add_filter( 'rank_math/frontend/title', function( $title ) { if ( is_product() && has_term( '36', 'product_cat' ) ) { $title = 'Photo of ' . $title; } return $title; }); add_filter( 'rank_math/frontend/breadcrumb/items', function( $crumbs ) { if ( is_product() && has_term( '36', 'product_cat' ) ) { $last_crumb_index = count( $crumbs ) - 1; $crumbs[ $last_crumb_index ][0] = 'Photo of ' . get_the_title(); } return $crumbs; }, 10, 2 ); add_filter( "rank_math/snippet/rich_snippet_product_entity", function( $entity ) { if ( is_product() && has_term( '36', 'product_cat' ) ) { $entity['name'] = 'Photo of '. get_the_title(); } else { $entity['name'] = get_the_title(); } return $entity; }); add_filter( 'rank_math/json_ld', function( $data ) { if ( is_product() && has_term( '36', 'product_cat' ) ) { $data['WebPage']['name'] = 'Photo of ' . get_the_title(); } else { $data['WebPage']['name'] = get_the_title(); } return $data; }, 9999, 2);
Here’s how you can add filter/hook to your WordPress site:
https://rankmath.com/kb/wordpress-hooks-actions-filters/We hope that helps, and please don’t hesitate to get in touch if you have any other questions.
Thank you.
Thank you, it worked
And I’m sorry because I have one more request
Can you tell me how to add products belonging to another category to this code with different title?
If you give just one example in this code, I will learn
example:
category id: 4426
title and schema title: %title% carSo that there is no ambiguity, I would say better, exactly like the previous one, only one more… Actually like this:
Title: %title% car %sep% %sitename%
category id: 4426
schema title: %title% carHello,
Please replace the previous code with the code below instead:
add_filter('rank_math/frontend/title', function ($title) { if (is_product() && has_term('36', 'product_cat')) { $title = 'Photo of ' . $title; }else if(is_product() && has_term('4426', 'product_cat')){ $title = RankMath\Helper::replace_vars('%title% car %sep% %sitename%'); } return $title; }); add_filter('rank_math/frontend/breadcrumb/items', function ($crumbs) { if (is_product() && has_term('36', 'product_cat')) { $last_crumb_index = count($crumbs) - 1; $crumbs[$last_crumb_index][0] = 'Photo of ' . get_the_title(); } return $crumbs; }, 10, 2); add_filter("rank_math/snippet/rich_snippet_product_entity", function ($entity) { if (is_product() && has_term('36', 'product_cat')) { $entity['name'] = 'Photo of ' . get_the_title(); } else if(is_product() && has_term('4426', 'product_cat')){ $entity['name'] = get_the_title() . ' car'; } else { $entity['name'] = get_the_title(); } return $entity; }); add_filter('rank_math/json_ld', function ($data) { if (is_product() && has_term('36', 'product_cat')) { $data['WebPage']['name'] = 'Photo of ' . get_the_title(); } else { $data['WebPage']['name'] = get_the_title(); } return $data; }, 9999, 2);
Don’t hesitate to get in touch with us if you have any other questions.
thank you big man, rankmath is the best
Hello,
Glad that helped.
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.
The ticket ‘meta title problem’ is closed to new replies.