Using wordpress advanced custom field plugin,
"Taxonomy equal category"
In the settings of, we have set the image to be registered in the category.

Parent category-on the page, link to the child category
Image image + title + link
I want to display the list with, and if there is no image, I want to display the thumbnail of the first article in the corresponding child category.

With the code below
Image image + title + link
Is displayed, but if there is no image, the part cannot be acquired properly.

I would appreciate it if you could teach me how to get it in the part.
I look forward to working with you.

<? php
$categories = get_categories (array (array (
        'child_of' =>$cat,
        'orderby' =>'term_order',
        'order' =>'ASC'
foreach ($categories as $category):
$cat_id = $category->cat_ID;
$post_id ='category_'. $Cat_id;
$catimg = get_field ('catalogue-img', $post_id);
$img = wp_get_attachment_image_src ($catimg,'thumbnail');
<? php if (empty ($catimg)) :?>

<!-I want to put thumbnails of child categories->
<span>category_nicename;?>"><? Php echo $category->cat_name;?></span>

<? php else:?>

" alt = "<? php echo $category->cat_name;?>" />
<span>category_nicename;?>"><? Php echo $category->cat_name;?></span>

<? php endif;?>
<? php endforeach;?>
  • Answer # 1

    We have not confirmed the operation, but how about using the following code?

    <? php
    $args = array (
        'posts_per_page' =>1,
        'category' =>$cat_id,
        'post_type' =>'post',
        'post_status' =>'publish',
    $posts_array = get_posts ($args);
    foreach ($posts_array as $post_arr) {
        $post_id_in_child_cat = $post_arr->ID;
        $post_img_url = get_the_post_thumbnail_url ($post_id_in_child_cat,'thumbnail');
        if (! empty ($post_img_url)) {
            echo' cat_name.'"/>';

Related articles