Andre Mesel
Andre Mesel

Reputation: 113

ACF - getfield() returns blank value

I got this WP installed with ACF Got a problem that getfield returns blank value.

Heres my Code in front-page.php

<?php
        $forside_slagord_stor = get_field('forside_slagord_stor');
        $forside_slagord_liten = get_field('forside_slagord_liten');
        $forside_slagord_liter = get_field('om_oss_tekst');
?>
    <header>
    <div class="container">
        <div class="intro-text">
            <div class="intro-lead-in"><?php echo $forside_slagord_stor;?></div>
            <div class="intro-heading"><?php echo $forside_slagord_liter;?></div>
            <a href="#services" class="page-scroll btn btn-xl">Tell Me More</a>
        </div>
    </div>
</header>

Here is my ACF setup. ACF setup

And here is ACF - forside setup ACF - forside setup

The result is that it returns empty value .

Thank you for your help

Upvotes: 2

Views: 1973

Answers (1)

Benoti
Benoti

Reputation: 2200

Without the second parameter in get_field, you need to be in the loop, and it's seems that you are not in.

If your code is at the top of the page, you need to add the post ID parameter, you need to use the global $post; to get it.

<?php
    global $post;

    $forside_slagord_stor = get_field('forside_slagord_stor', $post->ID);
    $forside_slagord_liten = get_field('forside_slagord_liten', $post->ID);
    $forside_slagord_liter = get_field('om_oss_tekst', $post->ID);
?>
<header>
<div class="container">
    <div class="intro-text">
        <div class="intro-lead-in"><?php echo $forside_slagord_stor;?></div>
        <div class="intro-heading"><?php echo $forside_slagord_liter;?></div>
        <a href="#services" class="page-scroll btn btn-xl">Tell Me More</a>
    </div>
</div>

Another answer about one of your comment : you don't need to include any file of acf, when the template is about to render, all the plugins files and assets are usually load.

Hope it helps!

Upvotes: 1

Related Questions