ACF - 根据用户状态更改帖子特色图像

发布于 2025-01-16 01:15:58 字数 832 浏览 3 评论 0原文

我创建了一个名为“main_image”的字段组,在它下面有 2 个字段 1. '主图像记录' 类型:图像

2。 'main_image_logout' type:image

我想做的是向所有人显示帖子附带的经典特色图像,并为

登录的用户在“main_image_logged”字段中显示图像以进行注销,我什至尝试设置 name:_thumbnail_id从“main_image_logout”获取图像并将其用作特色图像。

有什么办法可以做到这一点吗? 如果用户已注销->来自“main_image_logout”字段的特色图像 如果用户已登录->来自字段“main_image_logged”的特色图像

尝试了类似的操作,但

function acf_set_featured_image( $value, $post_id, $field  ){
if (is_user_logged_in()) {    
    if($value != ''){
        //Add the value which is the image ID to the _thumbnail_id meta data for the current post
        add_post_meta($post_id, '_thumbnail_id', $value);
    }
 
    return $value;
}
}
add_filter('acf/update_value/name=main_image_logged', 'acf_set_featured_image', 10, 3);

使用时是错误的: WordPress 高级自定义字段 Divi主题

非常感谢大家

I’ve created a field group called ‘main_image’ and under it I have 2 fields
1.
‘main_image_logged’
type: image

2.
‘main_image_logout’
type:image

what im trying to do is to show the classic featured image which comes with the post for all, and for users that is logged in show the image in field “main_image_logged’

for the logout i even tried to set name:_thumbnail_id which took the image from ‘main_image_logout’ and used it as a featured image.

is there any way how to do this?
if user is logged out -> featured image from field ‘main_image_logout’
if user is logged in -> featured image from field ‘main_image_logged’

tried something like this but it's wrong

function acf_set_featured_image( $value, $post_id, $field  ){
if (is_user_logged_in()) {    
    if($value != ''){
        //Add the value which is the image ID to the _thumbnail_id meta data for the current post
        add_post_meta($post_id, '_thumbnail_id', $value);
    }
 
    return $value;
}
}
add_filter('acf/update_value/name=main_image_logged', 'acf_set_featured_image', 10, 3);

using:
Wordpress
Advanced custom fields
Divi theme

thanks alot guys

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱花落人离去 2025-01-23 01:15:58

试试这个

add_filter('post_thumbnail_id', 'replace_thumbnail_id_with_acf', 20, 2);
function replace_thumbnail_id_with_acf($thumbnail_id, $post) {
    if ( is_user_logged_in() ) {
        $image_id = get_field('reveal_face', $post->ID, false);
        if ($image_id) {
          $thumbnail_id = $image_id;
        }
    } else {
        $image_id = get_field('_thumbnail_id', $post->ID, false);
        if ($image_id) {
          $thumbnail_id = $image_id;
        }
    }
    return $thumbnail_id;
}

Try this

add_filter('post_thumbnail_id', 'replace_thumbnail_id_with_acf', 20, 2);
function replace_thumbnail_id_with_acf($thumbnail_id, $post) {
    if ( is_user_logged_in() ) {
        $image_id = get_field('reveal_face', $post->ID, false);
        if ($image_id) {
          $thumbnail_id = $image_id;
        }
    } else {
        $image_id = get_field('_thumbnail_id', $post->ID, false);
        if ($image_id) {
          $thumbnail_id = $image_id;
        }
    }
    return $thumbnail_id;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文