Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php WordPress模板中显示ACF字段时出现问题_Php_Wordpress_Advanced Custom Fields - Fatal编程技术网

Php WordPress模板中显示ACF字段时出现问题

Php WordPress模板中显示ACF字段时出现问题,php,wordpress,advanced-custom-fields,Php,Wordpress,Advanced Custom Fields,好吧,我遇到了有史以来最奇怪的问题。我有一个自定义主页模板的WordPress网站,我正在工作 您可以看到一个屏幕截图,显示我如何在后端设置此模板的ACF字段 我遇到的问题是,当我试图显示字段时 <img src="<?php the_field('slide_1_thumb'); ?>" alt="" /> “alt=”“/> 图像根本没有显示。我将返回值设置为“image URL”,奇怪的是它显示了 <?php the_field('slide_1'); ?

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

您可以看到一个屏幕截图,显示我如何在后端设置此模板的ACF字段

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

<img src="<?php the_field('slide_1_thumb'); ?>" alt="" />
“alt=”“/>
图像根本没有显示。我将返回值设置为“image URL”,奇怪的是它显示了

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

很好,没有任何问题

我不确定我在这里做错了什么,但根本没有拼写错误,因为某些原因,那些幻灯片上的拇指图像不会出现。我想这是Occam的剃刀类型的情况之一,我错过了一些非常简单的事情,但我已经重复了一百万次,没有运气

下面是我的home-page.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(); ?>


使用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字段,然后用与以前完全相同的名称重新创建它们。果然解决了


因此,虽然我没有一个结论性的答案来解释为什么会发生这种情况,但我能说的是,这一定是某种奇怪的故障。

Joe,好消息,我应该从一开始就这样做。然而,这仍然没有足够奇怪地解决这个问题。
<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>