我正在使用自定义帖子类型在我的网站上显示横幅。内容可以是文本,图像和快捷代码(例如,按钮短码(。
如果我用短代码显示内容,则一切看起来都可以找到。横幅本身内部的短代码。
有什么方法可以渲染该快速代码?
这是我的汇编:
// [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>';
}
参考:do_shortcode