Reputation: 13
I had this wordpress functions
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');
In the code above if I type [randm]
will show the post from "Motor" category, I want to add multiple category in there and multiple shortcode for every different category how to do that?
Upvotes: 0
Views: 564
Reputation: 174
Your code seems to be incomplete, anyway: You need to use shortcode attritubes, if I'm understanding you right.
function randm($atts) {
$args = array(
'post_type' => 'post',
'orderby'=> 'rand',
'category_name' => $atts['category'],
'posts_per_page' => 1,
);
/// ... your code
}
You can use this as shortcode:
[randm category="Motor"]
If you want to use multiple categories, you can explode the string:
function randm($atts) {
$args = array(
'post_type' => 'post',
'orderby'=> 'rand',
'category_name' => explode(', ', $atts['category']),
'posts_per_page' => 1,
);
/// ... your code
}
and use
[randm category="Motor, Second"]
If you want to make sure a default value exist you can use
function randm($atts) {
$atts = shortcode_atts( array(
'category' => 'Motor',
), $atts);
// Remaining code
}
Upvotes: 1