问题
I have created a custom post type. Everything works fine with a shortcode like this [program]
Now I would like to be able to create a shortcode like this
[program category="category_1,category_2,category_3"]
All those categories slugs from the shortcode have to appear in a radio button filter with the categorie name. Its working with only one category in the shortcode but once they're more and with commas it doesn't.
<?php function program_shortcode( $atts ) {
ob_start();
extract( shortcode_atts( array (
'category' => '',
), $atts ) );
$options = array(
'post_type' => 'program',
'category_name' => $category,
'posts_per_page' => -1); ?>
<div id="program-radiobuttons">
<?php $cat->slug = $category; ?>
<?php $categories = get_taxonomies(); ?>
<?php $checked = false ?><?php foreach ( $categories as $tax_type_key => $taxonomy ) {
if ( $cat = get_term_by( 'slug', $cat->slug , $taxonomy ) ) {
break; }} ?>
<label><input type="radio" name="cat" value="<?php echo $cat->slug ?>"
<?php if (!$checked) echo ' checked="checked"' ?>>
<span><?php echo $cat->name; ?></span> </label>
<?php $checked = true ?></div>
回答1:
Use the explode
to split category attribute string into an array and then loop through it.
extract( shortcode_atts( array (
'category' => '',
), $atts ) );
$taxonomies = explode(',', $category);
$taxonomy_name = 'TAXONOMY-SLUG';
foreach ( $taxonomies as $taxonomy ) {
$cat = get_term_by( 'slug', $taxonomy , $taxonomy_name );
}
来源:https://stackoverflow.com/questions/58194723/radio-buttons-for-custom-post-type-categories-from-shortcode-comma-separated-val