1

我正在尝试使用简码和 ACF 在主页上显示图像。

我遇到的问题是代码正在为正确的帖子获取数据,但它显示的是图像地址的一行文本,而不是图像。当我查看页面源代码时,似乎 img 标签不起作用。

这是我的代码。任何建议将不胜感激。

function grab_home_image_init(){
    add_shortcode( 'grab_home_image', 'grab_home_image_cb' );
}
add_action('init', 'grab_home_image_init');


function grab_home_image_cb() { 

        extract( shortcode_atts(
            array(
                'numberposts' => 1,
                'post_type' => 'project',
            ),
            $atts,
            'grab_home_image'
        ) );

    $args = array (
            'post_type' => $post_type,
            'numberposts' => $numberposts, 
    );

    $ML_home_image = get_posts( $args );

    if( ! empty( $ML_home_image ) ){
        foreach ( $ML_home_image as $p ){
            $output = '<img src="' . the_field('main_image',$p->ID) . '">';
        }
    }
    return $output ?? '<strong>Error</strong>';
}
4

0 回答 0