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

问题描述 投票:0回答:1

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

wordpress filter categories
1个回答
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;
}
© www.soinside.com 2019 - 2024. All rights reserved.