我想用两个参数做一个短代码$number_posts
和$category_name
.
短代码正在调用另一个名为bg_make_post_grid
将这些参数用作变量。
我补充道wp_die()
查看函数是否被调用,它不会杀死WordPress,因此我认为函数没有被调用。
我做错了什么?
// Add Shortcode
add_shortcode( \'bg_recent_post_grid\', \'bg_recent_post_grid_shortcode\' );
function bg_recent_post_grid_shortcode( $atts ) {
// Attributes
$atts = shortcode_atts(
array(
\'category_name\' => \'uncategorized\',
\'number_posts\' => \'1\',
), $atts );
return bg_make_post_grid ( $atts[\'number_posts\'], $atts[\'category_name\'] );
}
function bg_make_post_grid ( $number_posts, $category_name ) {
$args = array(
\'numberposts\' => $number_posts,
\'category\' => $category_name,
\'orderby\' => \'post_date\',
\'order\' => \'DESC\',
\'post_type\' => \'post\',
);
$recent_posts = wp_get_recent_posts( $args );
wp_die();
}
SO网友:Trilok
请用下面的代码替换旧代码,它将显示它正在工作
add_shortcode( \'bg_recent_post_grid\', \'bg_recent_post_grid_shortcode\' );
function bg_recent_post_grid_shortcode( $atts ) {
// Attributes
$atts = shortcode_atts(
array(
\'category_name\' => \'uncategorized\',
\'number_posts\' => \'1\',
), $atts );
return bg_make_post_grid ( $atts[\'number_posts\'], $atts[\'category_name\'] );
}
function bg_make_post_grid ( $number_posts, $category_name ) {
$args = array(
\'numberposts\' => $number_posts,
\'category\' => $category_name,
\'orderby\' => \'post_date\',
\'order\' => \'DESC\',
\'post_type\' => \'post\',
);
$recent_posts = wp_get_recent_posts( $args );
print_r( $recent_posts); die;
}
echo do_shortcode(\'[bg_recent_post_grid]\');