Home Forums General Issues Same image on category page and all its child pages with ACF


Same image on category page and all its child pages with ACF

  • I’m trying to set same banner image on category and to all its child pages.

    I was able to set image on category page but can’t get child pages to fetch the same image.

    Field rules TAXONOMY TERM is equal to CATEGORIES and used return value as URL.

    Code on category page:

    $queried_object = get_queried_object();
    $taxonomy = $queried_object->taxonomy;
    $term_id = $queried_object->term_id; ?>

    <div id=”banner” style=”background-image:url(<?php the_field(‘featuredimage’, $taxonomy . ‘_’ . $term_id); ?>);”>

    Does anyone know how to get field content from parent category and show it on its child pages. Advises are greatly appreciated 🙂

  • Do you mean on sub categories or on posts in a category?

  • Hi John!!

    Sorry, that I wasn’t clear enough. I meant posts in a category 🙂

  • Assuming that you’ll be trying to get this outside of the loop. The first thing you need to do is get the post ID, you can do this the same way that you’re getting the term in your code

    $queried_object = get_queried_object();
    $post_id = $queried_object->ID;

    Then you need to get the post categories and loop through the categories and get the term ID and then get the field from one of them the same way you’re getting it above.

  • Hi John! Thank you for quick answer 🙂 I’m not very familiar with coding and I’m struggling to get this to work. I REALLY appreciate your effort and would be very grateful if you walked this through little more detailed manner. Sorry for the trouble.

    // added to single.php
    // if outside of "The Loop" we need to get the current post id
    $queried_object = get_queried_object();
    $post_id = $queried_object->ID;
    // gets a list of all post categories
    $categories = wp_get_post_categories($post_id);
    // you could also limit the list returned above by including
    // secong $args parameter for function, for more info see
    $url = false; // initialize $url value
    if (count($categories)) {
      // the post has one or more categories
      // loop through them and see if any has an image set for the field
      foreach ($categories as $category) {
        $value = get_field('featuredimage', $taxonomy.'_'.$category->term_id);
        if ($value) {
          // this category has an image
          // you could also do more checking here
          // if there are other conditions to
          $url = $value;
          // in this case, use the first category that has an image
          // once we find an image stop looking
        } // end if $value
      } // end foreach categories
    } // end if categories
    <div id="banner"<?php 
      if ($url) {
        ?> style="background-image: url(<?php echo $url; ?>);"<?php 
      } // end if url
  • Hi John!!

    Great solution!!

    I didn’t get any values to $taxonomy & $category->term_id so I changes $taxonomy to category and $category->term_id to $category

    $value = get_field(‘featuredimage’, category . ‘_’ . $category);

    if ($url) { -statement was very good. I added default image on CSS if category image hasn’t been set.

    John, I really appreciate you helping me!

  • sorry about the error, happens when I copy and paste and then forget to change something important.

Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘Same image on category page and all its child pages with ACF’ is closed to new replies.