获取基于ats的短代码的post ID


Get post ID for Shortcode based off Atts

我希望为我的帖子创建一个短代码,我可以输入一个$atts,这将是一个在我的自定义帖子类型的帖子,我可以使用这个段塞来获得该帖子的ID,这样我就可以在短代码中拉入元数据。

add_shortcode('stats', 'stats');
function stats($atts) {
array(
    'hero' =>'',
);
$HeroSlug = $atts['hero']; 
$HeroPostID = I need this to grab the post ID based off the the $atts 'hero' which is the post's slug
$output =  echo get_post_meta($HeroPostID 'hero-sub-name', true);
return $output;
}

在短代码中,我要输入[stats hero="illidan"] illidan是我想要获取ID的自定义帖子类型帖子的段线我只是不确定如何获取该段线并获取该帖子的ID所以我可以在$HeroPostID变量中使用

add_shortcode('stats', 'stats_func');函数stats_func($ ats) {

    extract( shortcode_atts( array(
        'hero' => ''
    ), $atts ) );
    if(strlen($hero) < 1){ return; }
    $the_slug = $hero;
    $args=array(
        'name' => $the_slug,
        'post_type' => 'post',
        'post_status' => 'publish',
        'numberposts' => 1
    );
    $my_posts = get_posts($args);
    if( $my_posts ) {
        $HeroPostID = $my_posts[0]->ID;
    }
}
我就是这么做的

您试过get_the_ID()吗?这将给出显示短代码的当前post ID