Home › Forums › General Issues › 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>
The topic ‘Custom Field Type Not Work on Certain Templates?’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.