Support

Account

Home Forums General Issues Display custom field from category on child single page

Solving

Display custom field from category on child single page

  • Hello,

    I have a custom field in my categories for an big image.

    In my category.php, I display this big image with this code:

    <?php $term = get_queried_object(); ?>
    <?php if(get_field('big_image', $term)): ?> 
    <?php $MyImage = wp_get_attachment_image_src(get_field('big_image', $term), 'Size--xl'); ?>
    <style type="text/css">
    .my-image {background-image: url(<?php echo $MyImage[0]; ?>);}
    </style>
    <?php endif; ?>

    What about if I want to display this big image in all the articles / single templates which belong to that category ?
    This code dœsn’t work anymore. I guess there’s something to say I want its parent category big image ?

    Thanks in advance for your help ! 🙂

  • I’m not sure what you mean, your title says “child single page” which leads me to believe you want to do this on a single post page… but your then you say that you want to get the image from the parent category.

    If you are on a single post, the first thing you need to do is get the terms for the post. https://developer.wordpress.org/reference/functions/get_the_terms/

    Once you have the terms and you want to get the top level term

    
    $top_term = $term;
    if ($term->parent) {
      // term has a parent
      $ancestors = get_ancestors($term_id, 'your-taxonomy-name-here', 'taxonomy'); 
      $top_term_id = array_pop($ancestors);
      $top_term = get_term($top_term_id, 'your-taxonomy-name-here');
    }
    if(get_field('big_image', $top_term)) {
      /// your code here
    }
    
  • Hello John,

    Thank you for your help.
    Indeed, I’m talking about a single post page.

    Do I need to specify that it’s a category from a custom post type ?
    So it’s a taxonomy called “destinations”.

    So I adapt your code like this in my template single-destinations.php, no ? :

    <?php $top_term = $term;
    if ($term->parent) {
      // term has a parent
      $ancestors = get_ancestors($term_id, 'destinations', 'taxonomy'); 
      $top_term_id = array_pop($ancestors);
      $top_term = get_term($top_term_id, 'destinations');
    }
    if(get_field('big_image', $top_term)) { ?>
      <p>Hello !</p>
    <?php } ?>

    Nothing happens… 🙁 Is there something wrong ?

  • With a single post page then you need to get the terms associated with the post, get_queried_object() will not help you.

    
    // WP loop
    while (have_posts()) {
      the_post();
      $term = false;
      $terms = get_the_terms($post->ID, 'detinations');
      // $terms is an array of term object
      // use the first term found
      if ($terms) {
        $term = $terms[0];
        if ($term->parent) {
          // term is not at the top level
          $ancestors = get_ancestors($term_id, 'destinations', 'taxonomy');
          $top_term_id = array_pop($ancestors);
          $term = get_term($top_term_id, 'destinations');
        }
      }
      // $term now holds the top level term of a hierarchical taxonomy
      // based on the first term found for this post
      // or false if no term is selected for this post
      if ($term) {
        // output fields from the term
        the_field('big_image', $term);
      }
    } // end while have_posts
    
  • Thank you John,

    You code gives me blank page (I saw and correct the forgotten “s” in the first “destinations” term).
    I don’t know where is the problem.

    The big_image is defined in the “swimming” category from the “destinations” custom post type/taxonomy.
    Maybe I was not clear enough on the point ?

    Sorry I’m really not good at php … 🙁

  • I can’t promise that code I post here is without typos as I’m typing it directly in this editor and I don’t generally test it on a site first. If you’re getting a blank page then you need to turn on debugging in WP to see what the errors are. https://wordpress.org/support/article/debugging-in-wordpress/

  • Ok, I understand.

    It says :

    Uncaught Error: Cannot use object of type WP_Error as array in /Users/Path/single-destinations.php:15

    Line 15 is :
    $term = $terms[0];

    Does it help ?

  • This is returning an error

    
    $terms = get_the_terms($post->ID, 'detinations');
    

    which generally means that the taxonomy does not exist

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

You must be logged in to reply to this topic.