Php 使用短代码显示WordPress页面内容并显示嵌入的短代码

Php 使用短代码显示WordPress页面内容并显示嵌入的短代码,php,wordpress,wordpress-theming,wordpress-shortcode,Php,Wordpress,Wordpress Theming,Wordpress Shortcode,我正在使用自定义帖子类型在我的网站上显示横幅。 内容可以是文本、图像,也可以是短代码,例如按钮短代码 如果我用我的短代码显示内容,一切看起来都很好。除了横幅本身内部的短代码 有没有办法呈现那个短代码 这是我的密码: // [banner id="" class=""] function shortcode_banner( $atts, $content ){ extract(shortcode_atts(array( 'id' => '', '

我正在使用自定义帖子类型在我的网站上显示横幅。 内容可以是文本、图像,也可以是短代码,例如按钮短代码

如果我用我的短代码显示内容,一切看起来都很好。除了横幅本身内部的短代码

有没有办法呈现那个短代码

这是我的密码:

// [banner id="" class=""]
function shortcode_banner( $atts, $content ){
    extract(shortcode_atts(array(
        'id'    => '',
        'class' => '',
    ), $atts));

    $banner_id  = $id;
    $content    = get_post_field('post_content', $banner_id);

        return '<div class="'.$class.'">'.wpautop($content).'</div>';

}
add_shortcode( 'banner', 'shortcode_banner' );
请尝试以下代码:

// [banner id="" class=""]
function shortcode_banner( $atts, $content ) {
    extract(shortcode_atts( array(
        'id'    => '',
        'class' => '',
    ), $atts ) );

    $banner_id = $id;
    $content   = get_post_field( 'post_content', $banner_id );

    return '<div class="'.$class.'">'.wpautop( do_shortcode( $content ) ).'</div>';
}
或者,如果您希望使用递归短代码:

function recursively_do_shortcode( $content ) {
    $content2 = $content;
    do{
       $content = $content2;
       $content2 = do_shortcode( $content );
    } while( $content2 !== $content ); // presumably you can test if shortcodes exist in content as well
    return $content2;
}

// [banner id="" class=""]
function shortcode_banner( $atts, $content ){
    extract( shortcode_atts( array(
        'id'    => '',
        'class' => '',
    ), $atts ) );

    $banner_id = $id;
    $content   = get_post_field( 'post_content', $banner_id );

    return '<div class="'.$class.'">'.wpautop( recursively_do_shortcode( $content ) ).'</div>';
}

参考资料:

@Cray您是否添加了“短代码”横幅、“短代码”横幅;?我没有把它包括在答案中,但你应该这样做,这是我的错。现在它工作得很好,谢谢!一个问题:你对递归的理解是什么shortcodes@Cray,我的意思是短代码,它在渲染时返回其他短代码,而这些短代码又必须被渲染,以此类推,直到没有其他短代码返回为止