您好我已尝试过以下
$my_postid = 12;//This is page id
$content_post = get_post($my_postid);
$content = $content_post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
我在id为12的页面中编写了短代码。它不起作用。请帮忙!! !
答案 0 :(得分:1)
您可以使用get_shortcode_regex
功能检查帖子内容并隔离短代码。将my_shortcode
替换为您尝试获取的短代码的实际标识符:
$content_post = get_post( 12 );
$content = apply_filters( 'the_content', $content_post->post_content );
$pattern = get_shortcode_regex();
preg_match( '/' . $pattern . '/s', $content, $matches );
if ( is_array( $matches ) && $matches[2] == 'my_shortcode' ) {
$shortcode = $matches[0];
echo do_shortcode( $shortcode );
}