Support

Account

Home Forums Front-end Issues get_fields() is fetching the wrong custom fields

Solved

get_fields() is fetching the wrong custom fields

  • I’ve created a custom theme for a photographer client which displays a large hero image on each page using a “jumbotron” div. My theme will enable my client to upload whichever images he pleases to be displayed on each page from the backend with a custom field group I have created called “Jumbotron images”.

    Now, I wrote some code to randomly display just one of the many jumbotron images that can be uploaded via the custom field on the ‘edit page’ for each page , each time the user refreshes the page. (Kind of like the effect that tumblr uses on their login homepage.) the code is the following

    
    <?php 
    $fields = get_fields(); //store all fields in variable
    $keys = array_keys($fields); //get just the keys from the fields available
    $fieldLen = count($fields) - 1; //returns $fields length -1 to index array
    
    ?>
    <div class="jumbotron" style="background:#ffffff url('<?php the_field($keys[rand(0, $fieldLen)]);?>') no-repeat center;"> 
     <header>
       <h1 class="text-center archive-jumbo-heading"><?php wp_title('');?></h1>
    </header>
    </div>
    

    the code keys into the URL path value of the image by generating a random number and sets the random URL obtained as the background image of the jumbotron DIV. this code works on ALL PAGES EXCEPT the blog listing page I have as home.php On the blog listing page, instead of displaying one of the images uploaded in the custom field group available to it on the backend, it displays the custom field images from the FIRST Blog post listed. After seeing the the get_fields() output, I saw that the function is fetching the custom fields available to the first single.php post, rather than the home.php blog page as I had specifically declared on the custom fields “location rules”.
    Any Idea as to why this strange behavior is happening? I’m very puzzled.

  • Hi @moralejf,

    Try using get_fields($post_id)

    where the $post_id is a page_id where the banner is working

    Cheers

  • Thanks for the quick response! Though passing the page’s post ID as a parameter did in fact make get_fields() behave accordingly by fetching the page’s custom fields, the page’s behavior is still very, very weird. For instance when I output the Array that get_fields() fetches, everything looks normal I get an array that looks like this:

    
    array(3) {
      ["jumbo_1"]=>
      string(96) "http://localhost:8888/juanturcios.com/wp-content/uploads/2014/01/father-and-son-juan-turcios.jpg"
      ["jumbo_2"]=>
      string(101) "http://localhost:8888/juanturcios.com/wp-content/uploads/2014/01/fashion-photography-juan-turcios.jpg"
      ["jumbo_3"]=>
      string(96) "http://localhost:8888/juanturcios.com/wp-content/uploads/2014/01/wedding-brides-green-forest.jpg"
    }
    
    

    That means get_fields() is working properly. HOWEVER, when I go in and key into this array randomly (and again, the same code works in all pages except my blog index listing page home.php) and use the code’s output to generate the link for the banner’s background image, it is blank. Upon inspecting the div I can see that the background: url(”); is in fact blank. So then I decided to instead try to code it manually and used intead the function the_field(‘jumbo_1’) passing it the first image’s field name key so it would maybe display and nothing happened– still blank. Puzzled by this I decided to pass in the field name of a custom field that is available ONLY for single.php blog posts: the_field(‘slider_image_1’); which enables the user to upload an image for an image slider on every blog post. And to my surprise it did show up!

    It’s so odd. It seems as if the blog listing page did not have access to its own custom fields (despite the fact that get_fields() is outputting the right field group array) and rather somehow the code can display fields that are not even allowed to be on the blog page.

    Any insights on this? Should I change my field location rule logic somehow?

  • Hi @moralejf

    The ‘home page’ or ‘blog page’ does not have a global $post object becuase the query is to load the posts.

    This means that a get_field call won’t work because ACF doesn’t know which page to load from.

    A solution for this is to check if the page is_home:
    http://support.advancedcustomfields.com/forums/topic/acf-fields-not-displaying-on-posts-page/

  • Thanks @elliot for your response. I had already implemented the solution you suggested but it was still not working. Turns out I had to pass in the $post_id variable into the the_field(); invocation in the inline style as the second parameter for it to work. Thanks for your help and thanks for a great plugin!

    Jose

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

The topic ‘get_fields() is fetching the wrong custom fields’ is closed to new replies.