WordPress:在其他功能中使用shortcode_atts


WordPress: Use shortcode_atts in other function

是否可以在另一个函数中使用shortcode_atts中的变量?这是我的想法:

张贴

[图库 IDS="1,2,3,...n"]

函数 get_gallery_ids()

//get the gallery-ID's from post
function get_gallery_ids($atts) {
extract(shortcode_atts(array(
'ids'   => ''
), $atts));
return $ids;
}

函数 explode_ids()

//example function with ids
function explode_ids($ids) {
$ids = explode(',' $ids);
}

我该如何实现它?回报只是回声。

更新

上面的代码是我自己的新gallery_shortcode的一部分。

remove_shortcode('gallery', 'gallery_shortcode');
add_shortcode('gallery', 'get_gallery_ids');

我有一个想法:

我使用WordPress Codex中的示例http://codex.wordpress.org/Shortcode_API

function bartag_func( $atts ) {
    global $post;   // this is new
    extract( shortcode_atts( array(
    'foo' => 'something',
    'bar' => 'something else',
), $atts ) );
    update_post_meta($post->ID, "gallery_used_atts", $atts); // this is new
return "foo = {$foo}";
}
add_shortcode( 'bartag', 'bartag_func' );

现在,您可以通过以下方式获得在特定帖子或页面上使用$atts

$att_values = get_post_meta( $post_id, "gallery_used_atts", true );

然后你可以把它们用于任何你想要的。