从shortcode属性获取自定义分类法

我正在编写一个短代码,用于过滤页面中的自定义分类法。 我有90%的工作,但我不能将属性从简码转换为有效的数组。我从get_taxonomies()函数中获得了所有分类法,但我希望从我的shortcode属性中获得分类法。

所以我尝试了get_taxonomies()函数,它可以输出并工作:

Array ( [opleiding_niveaus] => opleiding_niveaus [provincies] => provincies [portfolio_category] => portfolio_category [portfolio_field] => portfolio_field [testimonial_category] => testimonial_category [portfolio_tag] => portfolio_tag )

我现在拥有的是这个,而不是get_taxonomies():

$atts = shortcode_atts(
    array(
        'taxonomy' => 'nothing',),$atts);
    $ShortcodeAtts= esc_attr($atts['taxonomy']);
    $taxonomies = explode(',',$ShortcodeAtts);

并输出:

Array ( [0] => provincies [1] => opleiding_niveaus )

我的循环如下:

if  ($taxonomies) {
            foreach ($taxonomies  as $taxonomy ) {
                echo '<h4>'. $taxonomy. '</h4>';
                $terms = get_terms(array(
                    'taxonomy' => $taxonomy,'hide_empty' => false,));
                $count = count($terms);
                if ( $count > 0 ){
                    foreach ( $terms as $term ) {
                        $termlinks= get_term_link($term,$taxonomy);
                        ?> <a href="<?php echo $termlinks; ?>">
                        <?php echo "<div style='display: block'><input type='checkbox' id='".$term->name."'" . $term->name . "/><label for='".$term->name."'>"; echo $term->name?></label></div><?php
                    }
                }
            }
        }

我的代码的问题是shortcode atts数组的格式不正确。并在循环执行get_taxonomies()函数未执行的操作时给出错误。

从shortcode属性获取自定义分类法

所以我现在看起来像这样。 第一个分类法有效,但是第二个分类法无效

我的简码如下:[filter taxonomy="provincies,opleiding_niveaus"]

完整代码:

<?php
    add_action('init','loadonInit');

    function loadonInit() {
         add_shortcode('filter','filter');
    }

    function filter($atts) {

        $args = array('public' => true,'_builtin' => false);
        $atts=shortcode_atts(
        array(
            'taxonomy' => 'nothing',$atts);
        $ShortcodeAtts= esc_attr($atts['taxonomy']);
        $taxonomies = explode(',$ShortcodeAtts); 
        // $output = 'names';
        // $operator = 'and';
        // $taxonomies = get_taxonomies($args,$output,$operator); 
        print_r($ShortcodeAttsArray);
        if  ($taxonomies) {
            foreach ($taxonomies  as $taxonomy ) {
                echo '<h4>'. $taxonomy. '</h4>';
                $terms = get_terms(array(
                    'taxonomy' => $taxonomy,$taxonomy);
                        ?> <a href="<?php echo $termlinks; ?>">
                        <?php echo "<div style='display: block'><input type='checkbox' id='".$term->name."'" . $term->name . "/><label for='".$term->name."'>"; echo $term->name?></label></div><?php
                    }
                }
            }
        }
    }
?>

有什么解决办法吗?

daguai123456 回答:从shortcode属性获取自定义分类法

问题在于,移至简码会占用空格,因此在爆炸之前解决此问题,我们将从字符串中删除空格

add_action('init','loadonInit');

function loadonInit() {
     add_shortcode('filter','filter');
}

function filter($atts) {

    $args = array('public' => true,'_builtin' => false);
    $atts=shortcode_atts(
    array(
        'taxonomy' => 'nothing',),$atts);

    $ShortcodeAtts= esc_attr($atts['taxonomy']);
    //removing spaces here
    $ShortcodeAtts = str_replace(" ","",$ShortcodeAtts );
    $taxonomies = explode(',',$ShortcodeAtts); 

    $output = 'names';
    $operator = 'and';
    $taxonomies = get_taxonomies($args,$output,$operator); 

    if  ($taxonomies) {
        foreach ($taxonomies  as $taxonomy ) {
            echo '<h4>'. $taxonomy. '</h4>';
            $terms = get_terms(array(
                'taxonomy' => $taxonomy,'hide_empty' => false,));
            $count = count($terms);
            if ( $count > 0 ){
                foreach ( $terms as $term ) {
                    $termlinks= get_term_link($term,$taxonomy);
                    ?> <a href="<?php echo $termlinks; ?>">
                    <?php echo "<div style='display: block'><input type='checkbox' id='".$term->name."'" . $term->name . "/><label for='".$term->name."'>"; echo $term->name?></label></div><?php
                }
            }
        }
    }
}
本文链接:https://www.f2er.com/3139121.html

大家都在问