Support

Account

Home Forums General Issues Image Object and Image URL not working.

Helping

Image Object and Image URL not working.

  • I am a PHP novice and have the following problem and I really hope someone can help as I cannot find the answer in any of the support forums: I want a non-tech WP admin to be able to change the image on a home page of a WordPress 4.1 site.

    I have successfully created an Image Object field and Image metabox that works just fine on a test page, but every time I try to replace the existing $home_banner image variable in the live home page template, it doesn’t work. I have also tried an Image URL field but it just renders in HTML as <img src="h" alt="h">

    I have set up an Image Object field called ‘image-main’ and I want to use it to replace the existing variable $home_banner variable in the following code. Can anyone tell me how to edit the existing page template do this. Thanks. Existing live code snippet is below:

    
    $home_banner = get_stylesheet_directory_uri() . ‘/library/images/';
    
    if (is_mobile()) {
    $home_banner .= ‘home-banner-medium.jpg';
    }
    
    else { $home_banner .= ‘home-banner.jpg';
    
    }
    
    <script>window.home_banner = “<?php echo $home_banner; ?>”;</script>
    
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    <div class=”banner”>
    
    <h1>Bringing nature to you <span>wherever you are</span></h1>
    
    </div>
    

    The particular home page is here: http://inhouse-test.indoorgardendesign.com/

    Thanks in anticipation.

    SJL

  • Hi @sjl,

    Thanks for the post.

    For the Url return type, you can use the following code:

    $home_banner = get_field('field_name', 123); // 123 is the post id that the field is listed in.
    
    <script>window.home_banner = “<?php echo $home_banner; ?>”;</script>
    
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    <div class=”banner”>
    
    <h1>Bringing nature to you <span>wherever you are</span></h1>
    
    </div>
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Image Object and Image URL not working.’ is closed to new replies.