WordPress:在其他功能中使用shortcode_atts

时间:2013-09-20 11:41:12

标签: php wordpress gallery shortcode posting

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

过帐

[gallery 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');

1 个答案:

答案 0 :(得分:0)

我有一个想法:

我使用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 );

然后你可以随意使用它们。