原始问题使用get_tax_meta()
它不存在于WordPress core中,但可能是asker创建的自定义函数。在这种情况下,我们可以用get_term_meta()
.
您可以尝试以下操作:
class List_Category_Images extends Walker_Category {
function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
$saved_data = get_tax_meta( $category->term_id, \'image_field_id\', true );
$cat_name = apply_filters(
\'list_cats\',
esc_attr( $category->name ),
$category
);
$link = \'<a href="\' . esc_url( get_term_link( $category ) ) . \'" \';
if ( $args[\'use_desc_for_title\'] && ! empty( $category->description ) ) {
$link .= \'title="\' . esc_attr( strip_tags( apply_filters( \'category_description\', $category->description, $category ) ) ) . \'"\';
}
$link .= \'>\';
$link .= \'<img src="\' . $saved_data[\'src\'] . \'">\';
$link .= $cat_name . \'</a>\';
if ( ! empty( $args[\'show_count\'] ) ) {
$link .= \' (\' . number_format_i18n( $category->count ) . \')\';
}
if ( \'list\' == $args[\'style\'] ) {
$output .= "\\t<li";
$class = \'cat-item cat-item-\' . $category->term_id;
if ( ! empty( $args[\'current_category\'] ) ) {
$_current_category = get_term( $args[\'current_category\'], $category->taxonomy );
if ( $category->term_id == $args[\'current_category\'] ) {
$class .= \' current-cat\';
} elseif ( $category->term_id == $_current_category->parent ) {
$class .= \' current-cat-parent\';
}
}
$output .= \' class="\' . $class . \'"\';
$output .= ">$link\\n";
} else {
$output .= "\\t$link<br />\\n";
}
}
}
我所做的是修改
Walker_Category class 在这种情况下,函数实际上是创建到术语/类别的链接。在方法的顶部,我调用
get_tax_meta()
函数(我假设它可以工作,因为它没有内置到WordPress中)。然后,我将图像直接添加到类别名称之前:
$link .= \'>\';
$link .= \'<img src="\' . $saved_data[\'src\'] . \'">\';
$link .= $cat_name . \'</a>\';
现在,您只需在
wp_list_categories()
功能:
$args = array(
\'orderby\' => \'name\',
\'show_count\' => 0,
\'pad_counts\' => 0,
\'hierarchical\' => 1,
\'taxonomy\' => $tax,
\'walker\' => new List_Category_Images
);
wp_list_categories( $args );