0
votes

Help please to display images and titles of subcategories on parent category page. I create field with type "Image", return value "Image URL", rule "Taxonomy is equal to category". I add images for categories. How to display this images with category titles and category links in category.php? I found many samples of code for it, but there are not working. Thanks in advance!

Settings screenshot: https://prnt.sc/17s4x2n

This code shows all Categories, but I need show only subcategories of parent category:

<?php
    $categories = get_categories(array('taxonomy'=>'category','hide_empty'=>false));
            if($categories){
                foreach($categories as $cat){?>
                    <div class="cat">
                    <div class="name"><a href="<?php echo get_category_link($cat->term_id);?>"><?php echo $cat->name;?></a></div>
                    <?php if($imgcat=get_field('imgcat', $cat->term_id)){?><div class="image"><img src="<?php echo $imgcat;?>"/></div><?php }?>
            </div>
        <?php }?>
    <?php }?>
1
Show us the visual form of your settings, and the php code that you useUnbywyd
Added code and screen of settingsstackdo

1 Answers

0
votes

You're almost in the right direction, use the get_field function, please read documentation:

get_field($selector, [$post_id], [$format_value]);

  • $selector (string) (Required) The field name or field key.
  • $post_id (mixed) (Optional) The post ID where the value is saved. Defaults to the current post.

Then just use

$imgcat=get_field("imgcat", $cat->term_id)
<?php
    $categories = get_categories(array('taxonomy'=>'category','hide_empty'=>false));
    if(!empty($categories)):
        foreach($categories as $cat):?>
            <div class="cat">
                <div class="name"><a href="<?php echo get_category_link($cat->term_id);?>"><?php echo $cat->name;?></a></div>
                <?php if($imgcat_src=get_field('imgcat', $cat->term_id)):?>
                <div class="image"><img src="<?php echo $imgcat_src;?>"/></div>
                <?php endif; ?>
                <?php
                    $taxonomies = array( 
                        'category',
                    );
                    $args = array(
                        'parent'         => $cat->term_id
                    ); 
                    $subcategories = get_terms($taxonomies, $args);
                ?>
                <?php
                if(!empty(subcategories)): ?>
                <div class="subcats">
                    // foreach -> get_field()
                </div>
                <?php endif; ?>
            </div>
    <?php endforeach; ?>
<?php endif; ?>