来自简码逗号分隔值的自定义 post 类型类别的单选按钮

Radio buttons for custom post type categories from shortcode comma separated value

我创建了一个自定义 post 类型。使用这样的短代码一切正常 [程序]

现在我希望能够创建这样的简码

[节目类别="category_1,category_2,category_3"]

短代码中的所有这些类别 slug 都必须出现在具有类别名称的单选按钮过滤器中。它仅适用于简码中的一个类别,但一旦它们更多且使用逗号则无效。

<?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>

使用explode将类别属性字符串拆分成一个数组,然后循环遍历它。

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 );
}