获取“ wp_list_categories”过滤器中的类别ID

时间:2019-02-18 11:26:26

标签: wordpress filter categories

我在我的wordpress边栏中使用“类别” 小部件。 我已经使用ACF字段选择类别的背景色。 基于ACF字段值,我想为每个类别的锚标记添加唯一的类。

为此,我实现了以下代码。

function categories_list_filter ( $variable, $args ) {
   $term_meta = get_term_meta( 5, 'category_background', true);
   $variable = str_replace('<a ', '<a class="' . $term_meta  . '-text"', $variable);
   return $variable;
}
add_filter( 'wp_list_categories','categories_list_filter' );

如何在此过滤器中获取类别ID?

1 个答案:

答案 0 :(得分:0)

我似乎想获取这样的ID。

add_filter( 'wp_list_categories', 'custom_list_categories', 999, 2 );
function custom_list_categories( $output, $args ){
    $terms = get_categories( $args );
    $result = $output;
    if( $terms ):
        ob_start(); ?>
            <?php 
            foreach( $terms as $term ):
                $term_meta = get_term_meta( $term->term_id, 'category_background', true); ?>
                <li class="cat-item cat-<?php echo $term->term_id; ?>">
                    <a class="<?php echo $term_meta; ?>" href="<?php echo get_term_link( $term ); ?>"><?php echo $term->name; ?></a>
                </li>
                 <?php
            endforeach; ?>
        <?php 
        $result = ob_get_clean();
    endif;
    return $result;
}
相关问题