2

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');
4

1 回答 1

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 );

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

于 2014-03-19T19:19:17.913 回答