最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Shortcode for Custom Field of Media Attachment (to use with Featured Images)

programmeradmin3浏览0评论

I'm using ACF (Advance Custom Fields) for Media Uploads to store location information about photos. When I set a post Featured Image to a photo that has this extra field, I'd like to output the location information from this Custom Field.

Basically, I need a shortcode that will output an ACF Custom Field of the Current Post's Featured Image.

I'm using ACF (Advance Custom Fields) for Media Uploads to store location information about photos. When I set a post Featured Image to a photo that has this extra field, I'd like to output the location information from this Custom Field.

Basically, I need a shortcode that will output an ACF Custom Field of the Current Post's Featured Image.

Share Improve this question asked Feb 17, 2021 at 14:22 Rob JacksonRob Jackson 1 2
  • Your best bet is to ask the ACF developers how to go about doing this; ACF works different than standard WP and WPSE is intended for development/programming questions that are directly within WordPress and not third party methods. – Tony Djukic Commented Feb 17, 2021 at 18:45
  • I contacted the ACF developers who said to hire a freelancer – Rob Jackson Commented Feb 18, 2021 at 0:17
Add a comment  | 

1 Answer 1

Reset to default 0

The most basic option I can think of is this

add_shortcode('bt_featured_image_acf_location', 'bt_featured_image_acf_location');
function bt_featured_image_acf_location ($atts) {
    return get_field('location', get_post_thumbnail_id());
}

Change 'location' to your acf field name and thats it.

get_post_thumbnail_id() will get the current post featured image id.

Once you have the ID of the post (images are posts, they are of the post type, attachment) that you want to get the ACF fields from you can pass it to get_field() in the second argument and you're done.

Change it how ever you want and add as much logic as you need, this is the best I can do with the information you provided

EDIT

If you use this shortcode inside a post template single.php, this shortcode works fine.

If you would like to display it anywhere else you will need to pass the post id manually.

Below the edited shortcode that can accept post id

add_shortcode('bt_featured_image_acf_location', 'bt_featured_image_acf_location');
function bt_featured_image_acf_location ($atts) {
    $atts = shortcode_atts([
        'post_id' => get_the_ID()
    ], $atts);

    return get_field('location', get_post_thumbnail_id($atts['post_id']));
}

Now this shortcode will still work the same way, but now you can pass it the post id

regular way to call it

echo do_shortcode('[bt_featured_image_acf_location]')

call the shortcode with post id

echo do_shortcode('[bt_featured_image_acf_location post_id="5526"]')

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>