Currently, I have a shortcode that displays a specific set of posts by specifying the post ID inside the function. But I would like to change this so the user can specify the post IDs as shortcode attributes.
Currently
[fsgrid]
Desired:
[fsgrid id="1, 2, 3"]
Here is the current code
public function shortcode_handler($atts) {
$atts = shortcode_atts(
array(
'posts_per_page' => 100 ,
'orderby' => 'post__in',
'post__in' => array(1, 2, 3)
), $atts, 'fsgrid'
);
return $this->grid($atts);
}
How do I change it? Any help is much appreciated.
答案 0 :(得分:0)
explode
是您的最佳选择。首先提取您的shortcode
ids属性,然后将逗号分隔的字符串分解为数组。
请参阅:https://codex.wordpress.org/Function_Reference/shortcode_atts
public function shortcode_handler($atts) {
extract(shortcode_atts(array(
'id' => null
), $atts, 'fsgrid'));
$post_ids = explode(",", strval($id));
$args = array(
'posts_per_page' => 100 ,
'orderby' => 'post__in',
'post__in' => $post_ids
);
return $this->grid($args);
}
然后您称您为简码[fsgrid id="1,2,3"]