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

advanced custom fields - Image Shortcode from ACF ID

programmeradmin2浏览0评论

I would like to create a shortcode that displays the image from an ACF custom field which currently has the return format set to the Image ID. The shortcode needs to accept a post_id so it can get the field from a certain page.

So the shortcode would be:

[acf_featured_item post_id="88"]

I am aware that there is an ACF shortcode available, but there is already another location which is dependant on the return format as ID. So I cannot change this.

My Current code is:

function acf_featured_item_shortcode( $atts ) {
// Attributes
$atts = shortcode_atts(
    array(
        'post_id' => '',
    ),
    $atts
);
$image = get_field('featured_item', $post_id );
$size = 'medium'; // (thumbnail, medium, large, full or custom size)
if( $image ) {
$output = wp_get_attachment_image( $image, $size, "", array( "class" => "mx-auto d-block" ));
} 
return $output;
}
add_shortcode( 'acf_featured_item', 'acf_featured_item_shortcode' );

But this only works when you are on the page for that id. How do I get this shortcode to work on any page with any post_id input?

I would like to create a shortcode that displays the image from an ACF custom field which currently has the return format set to the Image ID. The shortcode needs to accept a post_id so it can get the field from a certain page.

So the shortcode would be:

[acf_featured_item post_id="88"]

I am aware that there is an ACF shortcode available, but there is already another location which is dependant on the return format as ID. So I cannot change this.

My Current code is:

function acf_featured_item_shortcode( $atts ) {
// Attributes
$atts = shortcode_atts(
    array(
        'post_id' => '',
    ),
    $atts
);
$image = get_field('featured_item', $post_id );
$size = 'medium'; // (thumbnail, medium, large, full or custom size)
if( $image ) {
$output = wp_get_attachment_image( $image, $size, "", array( "class" => "mx-auto d-block" ));
} 
return $output;
}
add_shortcode( 'acf_featured_item', 'acf_featured_item_shortcode' );

But this only works when you are on the page for that id. How do I get this shortcode to work on any page with any post_id input?

Share Improve this question edited Nov 7, 2019 at 13:32 iamonstage asked Nov 7, 2019 at 12:39 iamonstageiamonstage 1671 silver badge9 bronze badges 1
  • And what exactly is your question? Developing shortcodes is described in the Plugin Handbook. Please edit your question to make clear what the result should be, what it currently is and where exactly you're having troubles. – kero Commented Nov 7, 2019 at 12:48
Add a comment  | 

1 Answer 1

Reset to default 3

It's because you have forgot to use $atts['post_id'] please find below code.

function acf_featured_item_shortcode( $atts ) {
    // Attributes
    $atts = shortcode_atts(
        array(
            'post_id' => '',
        ),
        $atts
    );
    $post_id = $atts['post_id'];
    $image = get_field('featured_item', $post_id );
    $size = 'medium'; // (thumbnail, medium, large, full or custom size)
    if( $image ) {
        $output = wp_get_attachment_image( $image, $size, "", array( "class" => "mx-auto d-block" ));
    } 
    return $output;
}
add_shortcode( 'acf_featured_item', 'acf_featured_item_shortcode' );
发布评论

评论列表(0)

  1. 暂无评论