admin管理员组

文章数量:1125084

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

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

Share Improve this question asked Feb 13, 2024 at 9:36 NicoCaldoNicoCaldo 1472 silver badges9 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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');

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');

本文标签: filtersApply function only for specific post