简体   繁体   中英

Wordpress Advanced Custom Fields - display on PAGE template

I am using Advanced Custom Fields with Wordpress. I have set up a field and can display it on my homepage/front-page.php template like this...

<?php the_field('primary_tagline'); ?>

I want to use the same field on my page.php template, but when i drop in the same code, no results are returned. I don't understand why it works on one template but not the other. Do i need different code to show the same field results across multiple templates? Here is the code...

   <?php the_field('primary_tagline'); ?>

    <div id="primary" class="content-area">
        <main id="main" class="site-main">
            <?php while ( have_posts() ) : the_post(); ?>
                <?php get_template_part( 'template-parts/content', 'page' ); ?>
                <?php
                    if ( comments_open() || get_comments_number() ) :
                        comments_template();
                    endif;
                ?>
            <?php endwhile; ?>
        </main><!-- #main -->
    </div><!-- #primary -->

Is it a loop problem? ACF won't show outside a loop?

If you want to grab field value outside the loop you must provide post_id as second parameter to the function

the_field($field_name, $post_id); //prints value
$value = get_field( $field_name, $post_id ); //returns value

ACF - get_field()

ACF - the_field()

like this:

<?php $value = get_field( 'primary_tagline', 288 );
echo $value; ?>

A bit late to the game here I think but I thought I would chip in incase anyone else is having problems with this. Just tried to do exactly the same thing, and the following solved it for me in the equivalent to your question.

<?php global $wp_query;
$post = $wp_query->post; 
$variablename = get_field('primary_tagline', $post->ID);?>

You want to make a call to the wp query and find the current post ID, and then use a variable to find the field for that post ID (current page - or a specified ID if needed). The variable I can only presume keeps the request within the global loop query so returns the actual value outside the loop instead of just the post ID .

Then to display your field, you would simple call the variable.

<?php echo $variablename; ?>

    <div id="primary" class="content-area">
        <main id="main" class="site-main">
            <?php while ( have_posts() ) : the_post(); ?>
                <?php get_template_part( 'template-parts/content', 'page' ); ?>
                <?php
                    if ( comments_open() || get_comments_number() ) :
                        comments_template();
                    endif;
                ?>
            <?php endwhile; ?>
        </main><!-- #main -->
    </div><!-- #primary -->

I have tried it without using a variable and for some reason it only shows the numerical value of the post ID - which I have noticed across the web is a common problem with this question. Even on the ACF forums I have found many questions about this.

Hope that helps anyone else needing to do this.

Appending the the_field or _get_field with get_option should work..

<h1><?php the_field('heading', get_option('page_for_posts')); ?></h1>

sourced from their documentation https://www.advancedcustomfields.com/resources/value-loading-posts-page/

是的,它应该在帖子内部,因为该字段是帖子的一部分。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM