Ben McMahon
Ben McMahon

Reputation: 1

wordpress display categories with image

newbie to writing PHP here. Many hours of searching and don't know if I'm looking in the right places.

I have a custom taxonomy (directory) with custom post categories for the listings.

I am using Advanced Custom Fields to add an image to each custom category (directory-image).

I am currently displaying the custom categories perfectly fine within a custom directory page template using the code below.

$cat = get_query_var('cat'); $args = array(
'orderby' => 'id',
'child_of' => $cat,
'hide_empty' => 0,
'taxonomy' => directory_categories 
); 
$categories = get_categories($args);
foreach($categories as $category) {
echo '<a href="' . get_category_link( $category->term_id ) . '" class="cat-icon-view">' . "\n";
echo '<img src=" " width="90%" height="164"></img>' . "\n";
echo '  <h5>' . $category->name . '</h5>' . "\n";
echo '</a>' . "\n";
}$

It has space to put the image, however I am not confident at writing php markup to include the image that is outputted from the custom image field under the custom category.

<?php the_field('directory-image', false); ?>

I have read many pages on writing PHP within PHP but I wasn't able to make it work and I'm sure there's a better way: Help!

Write PHP code in PHP How to write php within html within php? Display Categories with Image in Multiple Pages of Homepage - Magento

Upvotes: 0

Views: 4029

Answers (1)

mokiSRB
mokiSRB

Reputation: 1162

If you already have added a custom image field to category taxonomy, you should access it via tax meta. For the reference use official doc.

foreach ($categories as $category){ $image = get_term_meta($category->term_id, 'directory-image', true); }

Upvotes: 1

Related Questions