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 badges2 Answers
Reset to default 0Turned 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
版权声明:本文标题:filters - Apply function only for specific post 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736652931a1946184.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论