【发布时间】:2022-01-28 14:30:07
【问题描述】:
我正在尝试创建一个从特定帖子中提取内容的简码。例如:[show_my_content show-id="90"]
这是我的代码。但有些不对劲:
// Creating Shortcode
function show_my_content_shortcode ($attr, $content = null){
global $post;
// Define Shortcode Attributes
$shortcode_args = shortcode_atts(
array(
'show-id' => '',
), $attr);
$showcontent = $shortcode_args['show-id'];
$post = get_post($showcontent);
$output = apply_filters( 'the_content', $post->post_content );
return $output;
}
add_shortcode( 'show_my_content', 'show_my_content_shortcode' );
?>
但是,如果我替换以下变量的值:
$showcontent = $shortcode_args['show-id'];
有一个帖子ID,然后就可以了
$showcontent = 90;
但是,这是没有意义的,因为我显然希望能够在短代码的参数中输入 ID,而不是直接在我的代码中。
我也尝试删除我的 $showcontent 变量,并改为这样做,但这也没有用:
$post = get_post($shortcode_args['show-id']);
$output = apply_filters( 'the_content', $post->post_content );
return $output;
【问题讨论】:
标签: php wordpress wordpress-theming