ACF字段显示在WordPress模板中的问题


Issue with ACF fields showing up in WordPress template

好吧,我遇到了有史以来最奇怪的问题。我有一个我正在处理的WordPress网站的自定义主页模板。

您可以在此处查看我如何在后端设置此模板设置的 ACF 字段的屏幕截图。

我遇到的问题是,当我尝试显示该字段时

<img src="<?php the_field('slide_1_thumb'); ?>" alt="" />

图像根本不显示。我将返回值设置为"图像URL",奇怪的是它显示了

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

很好,完全没有问题。

不确定我在这里做错了什么,但根本没有拼写错误,由于某种原因,这些slide_1_thumb图像不会出现。我想这是奥卡姆剃刀类型的情况之一,我错过了一些如此简单的东西,但我已经重复了一百万次,但没有运气。

下面您将找到我的主页.php模板的PHP代码副本。

<?php
/**
 * Template Name: Home Page
 *
 * A custom template for the home page.
 *
 * The "Template Name:" bit above allows this to be selectable
 * from a dropdown menu on the edit page screen.
 */
 get_header(); ?>
 <div id="slideshow-container">
<div id="slideshow" class="slideshow pcfcu-theme">
    <div class="slideshow-img" style="background-image: url(<?php      the_field('slide_1'); ?>);">
        <div class="slideshow-img-inner">
            <div class="slideshow-img-text">
                <h1><?php the_field('slide_1_title'); ?></h1>
                <p><?php the_field('slide_1_description'); ?></p>
            </div>
        </div>
    </div>
    <div class="slideshow-img" style="background-image: url(<?php the_field('slide_2'); ?>);">
        <div class="slideshow-img-inner">
            <div class="slideshow-img-text">
                <h1><?php the_field('slide_2_title'); ?></h1>
                <p><?php the_field('slide_2_description'); ?></p>
            </div>
        </div>
    </div>
    <div class="slideshow-img" style="background-image: url(<?php the_field('slide_3'); ?>);">
        <div class="slideshow-img-inner">
            <div class="slideshow-img-text">
                <h1><?php the_field('slide_3_title'); ?></h1>
                <p><?php the_field('slide_3_description'); ?></p>
            </div>
        </div>
    </div>
</div>
 </div>
 <div id="content-container">
<div id="content-container-inner">
    <div id="recent-blog-entries-container">
        <div id="recent-blog-entries">
            <header><h1>Recent Blog Entries</h1></header>
            <?php $postslist = get_posts('numberposts=2&order=DESC&orderby=date'); 
                foreach ($postslist as $post) : setup_postdata($post);
            ?> 
                <h2 class="rbe-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                <p class="rbe-posted-on">Posted on: <?php the_time(get_option('date_format')) ?></p>
                <div class="rbe-excerpt"><?php the_excerpt(); ?></div>
            <?php endforeach; ?>
        </div>
    </div>
    <div id="features">
        <header><h1>Features</h1></header>
        <a class="goTo1"><div class="feature-thumb"><img src="<?php the_field('slide_1_thumb'); ?>" alt="" /></div></a>
        <a class="goTo2"><div class="feature-thumb"><img src="<?php the_field('slide_2_thumb'); ?>" alt="" /></div></a>
        <a class="goTo3"><div class="feature-thumb"><img src="<?php the_field('slide_3_thumb'); ?>" alt="" /></div></a>
    </div>
</div>
 </div>
<?php get_footer(); ?>

另一件奇怪的事情是,如果您拍摄其中一张有效的 ACF 图像,例如

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

它可以完美地工作并输出图像,但是当您将其向下移动到代码的"功能"部分以查看它是否可以代替其中一个小功能缩略图时,它将在该部分代码中不起作用。几乎就像页面底部的"功能"部分有某种错误,但我在我的代码中根本没有看到任何错误。

换句话说,所有这些代码都完美运行,直到它的最后一部分与"功能"缩略图图像。您可以在网站上实时查看此内容 这里.

使用 get_posts(

) 创建新循环时,应使用 wp_reset_postdata() 函数将其结尾,以将循环的数据重置回 get_posts() 之前的状态。所以试试这个:

<div id="recent-blog-entries">
    <header><h1>Recent Blog Entries</h1></header>
    <?php $postslist = get_posts('numberposts=2&order=DESC&orderby=date'); 
    foreach ($postslist as $post) : setup_postdata($post); ?> 
        <h2 class="rbe-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
        <p class="rbe-posted-on">Posted on: <?php the_time(get_option('date_format')) ?></p>
        <div class="rbe-excerpt"><?php the_excerpt(); ?></div>
    <?php endforeach; ?>
    <?php wp_reset_postdata(); ?>
</div>

在循环后添加 wp_reset_postdata() 应该可以为您解决此问题。

好的,我不知道发生了什么,但是要解决此问题,我所要做的就是删除后端上的ACF字段,然后使用与以前完全相同的名称重新创建它们。果然修好了。

所以,虽然我对为什么会发生这种情况没有决定性的答案,但我只能说这一定是某种奇怪的故障。