dashingzahi
Forum Replies Created
-
To achieve this with RankMath, you can use the following code snippet in your theme’s functions.php file or a custom plugin:
phpCopy code
function custom_rankmath_seo_title( $title, $id ) { // Get the post object $post = get_post( $id ); // Check if it's a single post if ( is_singular( 'post' ) && ! empty( $post ) ) { // Get the post categories $categories = get_the_category( $post->ID ); // Check if the post has categories if ( $categories ) { // Loop through categories and check for specific ones foreach ( $categories as $category ) { switch ( $category->name ) { case 'Cars': $title = 'How to Drive a %title% %sep% %sitename%'; break; case 'Recipes': $title = 'Cooking %title% Recipe %sep% %sitename%'; break; case 'Phones': $title = 'How to Buy %title% %sep% %sitename%'; break; // Add more cases for other categories as needed } } } } return $title; } add_filter( 'rank_math/frontend/title', 'custom_rankmath_seo_title', 10, 2 );
This code uses the
rank_math/frontend/title
filter hook to modify the SEO title dynamically based on the post category. Adjust the category names and title formats as per your specific needs. You can add more cases for other categories following the same pattern.Remember to test this code in a safe environment first, and always have a backup of your code before making changes.
I am using same method for my tesla homes blog.