列出短码(DO_SHORTCODE)中的分类术语段

时间:2020-01-15 作者:rel_99

我想在我的短代码中列出分类术语。

这是我获取术语列表的方式(用逗号分隔):

<?php $terms = get_the_terms( $post->ID , array( \'shoptype\' ) );
                    $i = 1;
                    foreach ( $terms as $term ) {
                        $term_link = get_term_link( $term, array( \'shoptype\' ) );
                            if( is_wp_error( $term_link ) )
                            continue;
                            echo \'\' . $term->name . \'\';
                            echo ($i < count($terms))? ", " : "";
                            $i++; 
                    } ?>
但我试图将其应用于以下代码(taxonomy\\u terms=“..term->slug.”),但它只是不断重复每个术语的短代码:

<?php $terms = get_the_terms( $post->ID , \'shoptype\' ); 
            foreach ( $terms as $term ) {
                $term_link = get_term_link( $term, \'shoptype\' );
                if( is_wp_error( $term_link ) )
                continue;
                echo do_shortcode( \'[ajax_load_more post_type="shops" taxonomy="shoptype" taxonomy_terms="\' . $term->slug . \'" taxonomy_operator="IN" posts_per_page="9" button_loading_label="Loading Shops" button_label="View More" scroll="false" repeater="default" orderby="title" order="ASC"]\' );
            } 
        ?>
是否有办法在taxonomy\\u terms=”部分列出术语(用逗号分隔)?

1 个回复
最合适的回答,由SO网友:Andrea Somovigo 整理而成

这个do_shortcode() 函数当前位于foreach循环中。我不知道这个短代码的参数是什么,但如果它接受逗号分隔的术语slug值:

$terms = get_the_terms( $post->ID , \'shoptype\' ); 
$i=1;
$commaSeparatedvalues="";
foreach ( $terms as $term ) {
  $term_link = get_term_link( $term, \'shoptype\' );
  if( is_wp_error( $term_link ) )
    continue;
  $commaSeparatedvalues .= $term->slug;   
  $commaSeparatedvalues .= $i < count($terms) ? ", " : "";
  $i++;
 } 

 echo do_shortcode( \'[ajax_load_more post_type="shops" taxonomy="shoptype" taxonomy_terms="\' . $commaSeparatedvalues . \'" taxonomy_operator="IN" posts_per_page="9" button_loading_label="Loading Shops" button_label="View More" scroll="false" repeater="default" orderby="title" order="ASC"]\' );
所以基本上在foreach 并保留do_shortcode() foreach循环外的函数