How to add custom image in WordPress category using Advanced Custom Fields?

If you want to add a custom image for WordPress category using ACF, hope this tutorial will helpful for you. Let’s do start

Step 1. Please go to your dashboard Custom Fields and create a field following the screenshot

Step 2. After creating the field you can see an image option is available in the posts Category.

Step 3. Code for getting and displaying the image

<?php 

    $post_categories = get_terms( ['taxonomy' => 'category'] );

    $args = array(
        'show_option_all'    => '',
        'orderby'            => 'name',
        'order'              => 'ASC',
        'show_count'         => 0,
        'hide_empty'         => 1,
        'use_desc_for_title' => 1,
        'child_of'           => 0,
        'feed'               => '',
        'title_li'           => '',
        'feed_type'          => '',
        'feed_image'         => '',
        'exclude'            => '',
        'exclude_tree'       => '',
        'include'            => '',
        'hierarchical'       => 1,
        'echo'               => 1,
        'taxonomy'           => 'category'
    );
    $the_cat_query = get_categories( $args );

    foreach($the_cat_query as $key => $category) {
        if( function_exists('acf_add_options_page') ) {
            $image = get_term_meta( $category->term_id, 'post_cat_image', true );
        }else {
            $image = '';
        }
    }
?>

<!-- Image -->
<img src="<?php echo esc_url( $image ); ?>" alt="<?php echo esc_attr__('image', 'text-domain');?>">

That’s it! Thank you!

Leave a Reply

Your email address will not be published. Required fields are marked *