Support

Account

Home Forums General Issues Image field used in template not always returning image url

Solving

Image field used in template not always returning image url

  • Hi,

    I’m using an image custom field named “featured_header_image” with return value set to url and its being outputted in my page.php template. So each page has a different header image.

    the issue is some pages are working and returning the image url but others are just returning the page url. not sure whats happening with it.

    context wise, this piece of code is not within a loop, but i assumed as it was pulling information from each page it should find image field from each page and pull that image in.

    the site is currently only on local host so i can’t give you a link to the site.

    can anyone help?

    here is the code:
    <?php get_header(); ?>

    <div class=”parallax-bg medium-parallax-bg” data-stellar-background-ratio=”0.4″ style=”background-image: url(<?php the_field(‘featured_header_image’); ?>)”>

    <div class=”texture”></div>
    </div>

    on a page which does work with the image the code outputted looks like this:

    <div class=”parallax-bg medium-parallax-bg” data-stellar-background-ratio=”0.4″ style=”background-image: url(http://localhost:8888/christophergladwell/wp-content/uploads/2015/09/VN_VARKALA_013.jpg); background-position: 0% 0%;”>

    <div class=”texture”></div>
    </div>

    on other pages it gets returned like this:

    <div class=”parallax-bg medium-parallax-bg” data-stellar-background-ratio=”0.4″ style=”background-image: url(http://localhost:8888/christophergladwell/retreats/); background-position: 0% 0%;”>

    <div class=”texture”></div>
    </div>

    —- Versions —-
    ACF v4.4.3
    WP v4.3

  • i had a problem once when the name was too long, maybe try something like “fdhder_img”. i don’t really think that helps but in one case something like this solved the problem for me.

    or try
    <?php echo get_field('featured_header_image'); ?>

    or
    <?php echo get_field('fdhder_img'); ?>

  • Hi @ambrosegladwell

    Since your code is not in the loop it might also be a good idea to send the page ID along with it.

    
    <?php global $post; ?>
    <div class="parallax-bg medium-parallax-bg" data-stellar-background-ratio="0.4" style="background-image: url(<?php echo get_field('featured_header_image', $post->ID); ?>)">
    
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Image field used in template not always returning image url’ is closed to new replies.