Wrong hreflang is blog posts

#842410
  • Resolved Damien Vanhille
    Rank Math free

    In our blog posts, the hreflang attribute is false. Please see this screenshot: https://imgur.com/a/yghTUTZ
    “en-us” version (current one) is correct, but “en” should have “/en/” inside the URL, and same for “fr” with “/fr/”.
    When looking the “en” version, then the “en” url is correct but not the “en-us” anymore : https://imgur.com/a/gNQ3Z9K

    We are using WPML for translation.
    It’s working for all the other post types (pages and custom ones).

Viewing 1 replies (of 1 total)
  • Hello,

    We apologize for the issue you are facing with the hreflang attributes in your blog posts. Thank you for raising this concern.

    It seems that the incorrect hreflang entries for your blog posts may be related to how WPML is handling the translations. Since you mentioned that it works correctly for other post types, we recommend checking the settings in WPML to ensure that the language URL format is set correctly for your blog posts.

    Additionally, you can try adding the following code snippet to your theme’s rank-math.php file to help correct the hreflang entries:

    if ( 'post' === get_post_type() ) {
        add_filter( 'rank_math/sitemap/entry', 'compsupp_5514_filter_sitemap_home_page_entry', 10, 3 );
    }
    function compsupp_5514_filter_sitemap_home_page_entry( $url, $type, $post ) {
        global $sitepress;
        static $wpml_home_urls = null;
        static $frontpage_translations = null;
        static $default_frontpage_id = null;
        if ( ! $wpml_home_urls ) {
            $active_languages = $sitepress->get_active_languages();
            $wpml_home_urls   = [];
            foreach ( $active_languages as $code => $language ) {
                $wpml_home_urls[ $code ] = $sitepress->language_url( $code );
            }
            $default_frontpage_id   = get_option( 'page_on_front' );
            $frontpage_trid         = $sitepress->get_element_trid( $default_frontpage_id, 'post_page' );
            $frontpage_translations = array_map( WPMLFPObj::prop( 'element_id' ), $sitepress->get_element_translations( $frontpage_trid, 'post_page' ) );
        }
        if ( in_array( $post->ID, $frontpage_translations ) && $post->ID !== $default_frontpage_id  ) {
            $language_code = array_search( $post->ID, $frontpage_translations );
            $url['loc']    = $wpml_home_urls[ $language_code ];
        }
        return $url;
    }

    Please ensure to take a backup of your website’s files and database before making any changes to the code.

    If the issue persists after trying these steps, please reply to this ticket, and a human agent from the Rank Math Support team will assist you further.

    Thank you for your understanding, and we hope this helps resolve the issue!

    Sources:
    Multilingual SEO with Rank Math SEO & WPML » Rank Math

    This is an automated reply from Rank Math Support Bot. If you need further assistance, please reply to this topic and a human support agent will reply to you shortly.

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

The ticket ‘Wrong hreflang is blog posts’ is closed to new replies.