Support

Account

Home Forums General Issues Custom Field Type Not Work on Certain Templates?

Solved

Custom Field Type Not Work on Certain Templates?

  • I’m using this very code on WordPress page templates, which works just fine.

    <div class="header-background">
    				<?php            
    					$image = get_field('background_image');            
    						if( !empty($image) ): ?>            
                                <div style="background: url('<?php echo $image['url']; ?>');"></div>
    				<?php endif; ?>
                </div>

    But there’s a different template used by another plugin that the above code doesn’t seem to post the custom field:
    `get_header();

    $evOpt = evo_get_options(‘1′);

    $archive_page_id = evo_get_event_page_id($evOpt);
    echo “<div id=’primary’ class=’site-content’>”;
    echo “<div id=’content’ role=’main’>”;
    ?>
    <div class=”header-background”>
    <?php
    $image = get_field(‘background_image’);
    if( !empty($image) ): ?>
    <div style=”background: url(‘<?php echo $image[‘url’]; ?>’);”></div>
    <?php endif; ?>
    </div>
    <?php

    // check whether archieve post id passed
    if($archive_page_id){

    $archive_page = get_page($archive_page_id);
    echo “<div class=’container’>”;
    echo apply_filters(‘the_content’, $archive_page->post_content);
    echo “</div>”;
    echo “</div>”;
    echo “</div>”;

    } else {
    echo “<p>ERROR: Please select a event archive page in eventON Settings</p>”;
    }

    get_footer();`

    So I’m not sure why the custom field won’t post on this template, as I don’t see much difference.

  • Hi @toad78

    That’s because you executed the get_field() function outside of The Loop. If you want to do it, you need to pass the post/page ID as the second parameter of the get_field() function like this:

    $image = get_field('background_image', 99);

    Where “99” is the ID of your post/page. This page should give you more idea about it: https://www.advancedcustomfields.com/resources/get_field/.

    Hope this helps ๐Ÿ™‚

  • Thank you.

    This worked:

    <div class="header-background">
    <?php 
    $image = get_field('background_image', 445);            
    if( !empty($image) ): ?>            
    <div style="background: url('<?php echo $image; ?>');"></div>
    <?php endif; ?>
    </div>
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.