Support

Account

Home Forums ACF PRO Category images

Solving

Category images

  • Hi all,

    I’m currently working on website in which I need to add images to categories. I got a flexible content field named content, with multiple fields in there. There’s in image field, which you’ll probably guess, let’s you insert an image.

    Adding the image to the category in my loop.php get’s tricky. I managed to display all sub-categories of a certain category (the subcategories contain the images), but are not able to print any image.

    Beneath a snippet of my loop.php

      <?php if (have_rows('zichtbare_diensten')) :
            $term_array = array();
            while (have_rows('zichtbare_diensten')) : the_row();
              $term_id = get_sub_field('zichtbare_diensten');
              $term = get_category($term_id);
              $term_name = $term->name;
              array_push($term_array, $term);
            endwhile;
            endif;
      ?>
    
      <?php if (get_row_layout() === 'diensten'): ?>
        <div id="diensten-thumbs">
        <?php foreach ($term_array as $row): ?>
          <div class="dienst" style="background-image: url(<?php the_sub_field('image', $row->term_id) ?>)">
            <h3><?php echo $row->name ?></h3>
          </div>
        <?php endforeach; ?>
        </div>
    <?php endif; ?>
  • In this part of your code:

    <?php the_sub_field('image', $row->term_id) ?>

    You need to add the taxonomy to the term id. Assuming that it is “category”

    <?php the_sub_field('image', 'category_'.$row->term_id) ?>

  • Hi John,

    Thanks for your suggestion, but it isn’t working unfortunately.
    This is my current code.

    
      <?php if (get_row_layout() === 'diensten'): ?>
        <div id="diensten-thumbs">
        <?php foreach ($term_array as $row): ?>
          <div class="dienst" style="background-image: url(<?php the_sub_field('image', 'category_'.$row->term_id) ?>)">
            <h3><?php echo $row->name ?></h3>
          </div>
        <?php endforeach; ?>
        </div>
      <?php endif; ?>
    
  • We need to do some debugging so that we can see what’s actually going on. The code you have looks like it should be working.

    Alter your code to add the output statements I’ve added below and let me know what gets output

    
    <?php if (have_rows('zichtbare_diensten')) :
            $term_array = array();
            while (have_rows('zichtbare_diensten')) : the_row();
              $term_id = get_sub_field('zichtbare_diensten');
              // display $term_id to see what it contains
              echo '<br />TERM ID: '; var_dump($term_id);
              $term = get_category($term_id);
              // display $term and see what it contains
              echo '<br />TERM: '; var_dump($term);
              $term_name = $term->name;
              array_push($term_array, $term);
            endwhile;
            endif;
      ?>
    
      <?php if (get_row_layout() === 'diensten'): ?>
        <div id="diensten-thumbs">
        <?php foreach ($term_array as $row): ?>
          <div class="dienst" style="background-image: url(<?php the_sub_field('image', $row->term_id) ?>)">
            <h3><?php echo $row->name ?></h3>
          </div>
        <?php endforeach; ?>
        </div>
    <?php endif; ?>
    
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Category images’ is closed to new replies.