• Resolved isshmen

    (@isshmen)


    I need to automate SEO titles on my blog based on the category the article is posted on.

    default code is: %title% %sep% %sitename%

    Let’s say I have some categories like: Cars, Recipes, Phones, etc.

    If my article posted in Cars is titled with “Tesla“, then RankMath title will be “How to Drive a Tesla – mysite.com”

    How to Drive a %title% %sep% %sitename% – if in Cars

    If my article posted in Phones is titled with “iPhone 15“, then RankMath title will be “How to buy iPhone 15 – mysite.com”

    How to buy %title% %sep% %sitename% – if in Phones

    …and so on.

    Thank you

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support Rank Math Support

    (@rankmathsupport)

    Hello @isshmen,

    Thank you for contacting Rank Math support.

    You will have to modify and use the following filter to set the title based on the category of the post:

    add_filter( 'rank_math/frontend/title', function( $title ) {
    global $post;
    $term = get_the_category($post->ID);
    if (is_single()) {
    if (in_array('Cars', wp_list_pluck($term, 'name'))) {
    $title = "How to drive ".$title;
    } else if (in_array('Phones', wp_list_pluck($term, 'name'))){
    $title = "How to buy ".$title;
    }
    return $title;
    }
    });

    Here is how to add a filter to your site:?https://rankmath.com/kb/wordpress-hooks-actions-filters/

    Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.

    Thread Starter isshmen

    (@isshmen)

    It works, thanks a lot.

    How about if I want to put something AFTER $title?

    $title = “How to buy “.$title; CHEAP.

    I have tried with this code but is not working:

    $title = “How to buy $title CHEAP” – and different methods.

    Thanks again.

    Plugin Support Rank Math Support

    (@rankmathsupport)

    Hello @isshmen,

    You need to use the dot or period operator to concatenate words/strings in PHP.

    Here’s the revised code:
    add_filter('rank_math/frontend/title', function ($title) { global $post; $term = get_the_category($post->ID); if (is_single()) { if (in_array('Cars', wp_list_pluck($term, 'name'))) { $title = "How to drive " . $title; } else if (in_array('Phones', wp_list_pluck($term, 'name'))) { $title = "How to buy " . $title . " CHEAP"; } return $title; } });
    Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.

    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.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom Single Title based on Category’ is closed to new replies.