问题描述
function randm() {
$args = array( 'post_type' => 'post','orderby'=> 'rand','category_name' => 'Motor','posts_per_page' => 1,);
wp_reset_postdata();
}
else { $string .= 'no posts found'; } return $string; }
add_shortcode('randm','randm');
add_filter('widget_text','do_shortcode');
在上面的代码中,如果我键入[randm]
将显示“ Motor”类别中的帖子,我想在其中添加多个类别,并为每个不同的类别添加多个简码怎么做?
解决方法
无论如何,您的代码似乎不完整: 如果我理解正确,则需要使用简码attritube。
function randm($atts) {
$args = array(
'post_type' => 'post','orderby'=> 'rand','category_name' => $atts['category'],'posts_per_page' => 1,);
/// ... your code
}
您可以将其用作简码:
[randm category="Motor"]
如果要使用多个类别,则可以爆炸字符串:
function randm($atts) {
$args = array(
'post_type' => 'post','category_name' => explode(',',$atts['category']),);
/// ... your code
}
并使用
[randm category="Motor,Second"]
如果要确保存在默认值,可以使用
function randm($atts) {
$atts = shortcode_atts( array(
'category' => 'Motor',),$atts);
// Remaining code
}