I'm using the following code to display a list of my taxonomy terms in my home page:
$customPostTaxonomies = get_object_taxonomies( 'guide' );
if( count( $customPostTaxonomies ) > 0 )
{
foreach( $customPostTaxonomies as $tax )
{
$args = array(
'orderby' => 'name',
'show_count' => 0,
'pad_counts' => 0,
'hierarchical' => 1,
'taxonomy' => $tax,
);
wp_list_categories( $args );
}
}
Now i'm trying to add to the title, the term thumbnail that i had setup with a custom field to this list ( that i can output with the following code ):
$saved_data = get_tax_meta( $term_id, 'image_field_id', true );
echo '<img src="' . $saved_data['src'] . '">';
I really don't know how to mix them together... I've read about custom walker class but I don't know how to use it. Can anybody show me an example of how to achieve this?
I'm using the following code to display a list of my taxonomy terms in my home page:
$customPostTaxonomies = get_object_taxonomies( 'guide' );
if( count( $customPostTaxonomies ) > 0 )
{
foreach( $customPostTaxonomies as $tax )
{
$args = array(
'orderby' => 'name',
'show_count' => 0,
'pad_counts' => 0,
'hierarchical' => 1,
'taxonomy' => $tax,
);
wp_list_categories( $args );
}
}
Now i'm trying to add to the title, the term thumbnail that i had setup with a custom field to this list ( that i can output with the following code ):
$saved_data = get_tax_meta( $term_id, 'image_field_id', true );
echo '<img src="' . $saved_data['src'] . '">';
I really don't know how to mix them together... I've read about custom walker class but I don't know how to use it. Can anybody show me an example of how to achieve this?
Share Improve this question edited Dec 8, 2014 at 17:29 Howdy_McGee♦ 20.9k24 gold badges91 silver badges177 bronze badges asked Dec 8, 2014 at 13:42 olivierolivier 111 silver badge5 bronze badges1 Answer
Reset to default 2The original question uses get_tax_meta()
which does not exist in WordPress core but may be a custom function created by the asker. In this case we could switch it out just as well with get_term_meta()
.
You could try this:
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";
}
}
}
What I'm doing is modifying one of the methods inside the Walker_Category class which in this case the function is actually creating the link to the term / category. At the top of the method I call the get_tax_meta()
function ( which I assume works, as this is not built into WordPress ). Then I add the image directly before the name of the category:
$link .= '>';
$link .= '<img src="' . $saved_data['src'] . '">';
$link .= $cat_name . '</a>';
Now all you need to do is define a new instance of this class in your wp_list_categories()
function:
$args = array(
'orderby' => 'name',
'show_count' => 0,
'pad_counts' => 0,
'hierarchical' => 1,
'taxonomy' => $tax,
'walker' => new List_Category_Images
);
wp_list_categories( $args );
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745149959a4613822.html
评论列表(0条)