Skip to content

Instantly share code, notes, and snippets.

@MikeiLL
Last active November 19, 2018 23:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save MikeiLL/d810c46009a3f91a564c40b37e9f6145 to your computer and use it in GitHub Desktop.
Save MikeiLL/d810c46009a3f91a564c40b37e9f6145 to your computer and use it in GitHub Desktop.
<?php
/**
* Walker Class for List Category which also pulls in image from ACF Field
* source: https://wordpress.stackexchange.com/a/170603/48604
*
*/
class List_Category_Walker extends Walker_Category {
function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
$cat_name = apply_filters(
'list_cats',
esc_attr( $category->name ),
$category
);
$link = '<a href="' . esc_url( get_term_link( $category ) ) . '" ';
$link .= '>';
// Get array of images from ACF Image field
$image_array = get_field('main_category_image', 'term_' . $category->term_id);
// But if that's not set set it to image from latest post within category
if (empty($image_array['sizes']['medium'])):
// Get one post
$args = array(
'post_type' => 'project',
'tax_query' => [
'taxonomy' => 'project-category',
'terms' => $category->term_id,
],
'posts_per_page' => 1,
'orderby' => 'date',
'order' => 'ASC'
);
$query = new WP_Query($args);
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
global $post;
$image = get_the_post_thumbnail_url( $post->ID, 'medium', null );
}
wp_reset_postdata();
} else {
// some default image
}
else:
$image = $image_array['sizes']['medium'];
endif;
$link .= '<img src="' . $image . '" alt="'. $category->name .'">';
$link .= "<br/>" . $cat_name . '</a>';
if ( ! empty( $args['show_count'] ) ) {
$link .= ' (' . number_format_i18n( $category->count ) . ')';
}
$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";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment