简码逗号分隔值中用于自定义帖子类型类别的单选按钮

时间:2019-10-02 03:07:51

标签: wordpress radio-button custom-post-type shortcode

我创建了一个自定义帖子类型。像这样的[程序]

短代码,一切都可以正常工作

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

[程序category =“ category_1,category_2,category_3”]

所有这些来自简码的类别标记都必须出现在具有类别名称的单选按钮过滤器中。它只与短代码中的一个类别一起使用,但是一旦它们更多且与逗号一起使用,便不会起作用。

<?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 个答案:

答案 0 :(得分:0)

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