0

嗨我试过如下

$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 的页面中编写了简码。它不起作用。请帮忙!!!

4

1 回答 1

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 );
}
于 2013-05-01T13:07:01.807 回答