I’m trying to list the relevant categories for a custom taxonomy when visiting a post. At the moment, I’m trying to use the below code, which currently lists all of the categories, rather than the relevant ones, and the image url is only the taxonomy ID.
Can anyone help me work out where I’ve been stupid please?
$args = array( 'hide_empty' => '0','taxonomy' => 'book_cat');
$categories = get_categories($args);
if($categories){
echo '<ul>';
foreach($categories as $category) {
$link = get_term_link($category);
echo '<li>';
$size = "thumbnail";
$image = get_field('main_image', 'category_'.$category->term_id);
echo '<img src="' . $image . '" />';
echo '<span class="cat-title"><a href="'.$link.'">' . $category->name . '</a></span>';
echo '</li>';
}
echo '</ul>';
}
You should use wp_get_post_categories() to get the categories related to a post.
Inside the loop of terms you should use
$image = get_field('main_image', $category);
What you do with that depends on what you have the return value of the image field set to https://www.advancedcustomfields.com/resources/image/