0

I have a function to change the locale of the posts

function change_og_locale( $locale ) {
    return 'es_ES';
}
add_filter( 'wpseo_locale', 'change_og_locale' );

Now, I would like to fire it only if the post has a certain ID. I've tried with

function change_og_locale($locale ) {
    if ( $post_id == 52397 ) {
        return 'es_ES';
    }
    return $locale;
}
add_filter('wpseo_locale', 'change_og_locale');

But it seems not working

2 Answers 2

0

Turned out that to check the post_id the function is is_single

function change_my_locale($locale ) {
    if ( is_single(52397) ) {
        return 'es_ES';
    }
    return $locale;
}
add_filter('locale', 'change_my_locale');
0

In order to access the post ID within the change_og_locale function, you need to use the WordPress global variable $post or the get_the_ID() function to retrieve the current post ID

function change_og_locale($locale) {
    global $post; // Access the global $post variable
    
    if (is_singular() && $post->ID == 52397) {
        return 'es_ES';
    }
    
    return $locale;
}
add_filter('wpseo_locale', 'change_og_locale');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.