ACF if 语句在字段为空时不起作用

我有一个用于英雄图像的ACF字段,称为。此字段位于我的页面顶部,如下所示:hero_imagesingle.php


<?php

/**

 * The template for displaying all single posts

 *

 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/#single-post

 *

 * @package sitename

 */


get_header();

?>


<?php 

    $post_id = get_the_ID(); // Required as outside the loop

    $image = get_field('hero_image', $post_id);

    if ($image) {

        echo '<div class="hero">'.wp_get_attachment_image( $image, 'hero').'</div>';

    }

?>


<div class="has-sidebar">


    <div id="primary" class="content-area">


        <main id="main" class="site-main">


        <?php

        while ( have_posts() ) :

            the_post();


            get_template_part( 'template-parts/content', get_post_type() );


            the_post_navigation();


        endwhile; // End of the loop.

        ?>


        </main><!-- #main -->


    </div><!-- #primary -->


    <?php

        get_sidebar();

        get_footer();

    ?>


</div><!-- .has-sidebar -->

我使用变量从循环外部获取字段。图像按预期加载。$post_id


如果尚未使用该字段为帖子上传图像,我希望前端没有标记。但是,我仍然看到以下内容:


<div class="hero"></div>

为什么我的 if 语句在字段未使用时不起作用?


元芳怎么了
浏览 123回答 1
1回答

紫衣仙女

我做了一些进一步的挖掘,发现了我的问题。我有两个 ACF 字段和 .这些设置显示在所有页面和帖子上,包括自定义帖子类型。hero_imagethumbnail_image查看该文件,以下是我的发现:header.php<?php&nbsp; &nbsp; // Get post ID&nbsp; &nbsp; $post_id = get_queried_object_id();&nbsp; &nbsp; // Hero image&nbsp; &nbsp; $hero = get_field('hero_image', $post_id);&nbsp; &nbsp; $hero_url = wp_get_attachment_url( get_field('hero_image', $post_id), 'hero');?><?php if ( is_single() || is_archive() ): ?>&nbsp; &nbsp; <header id="masthead" class="site-header"><?php else: ?>&nbsp; &nbsp; <header id="masthead" <?php if ($hero) { echo 'class="site-header has-background" style="background:url('.$hero_url.')"'; } else { echo 'class="site-header"'; } ?>><?php endif; ?>如您所见,我还在循环外部使用变量。这阻止了第二个变量通过 工作。$post_id$post_idsingle.php我已将标题.php重命名为.然后,我通过此变量使用它,因为它也在循环之外。这已经解决了这个问题。$post_id$post_id_outside_loopsingle.php<?php/**&nbsp;* The template for displaying all single posts&nbsp;*&nbsp;* @link https://developer.wordpress.org/themes/basics/template-hierarchy/#single-post&nbsp;*&nbsp;* @package sitename&nbsp;*/get_header();?><?php&nbsp;&nbsp; &nbsp; // $post_id_outside_loop is set via header.php&nbsp; &nbsp; $image = get_field('hero_image', $post_id_outside_loop);&nbsp; &nbsp; if ($image) {&nbsp; &nbsp; &nbsp; &nbsp; echo '<div class="hero">'.wp_get_attachment_image( $post_id_outside_loop, 'hero').'</div>';&nbsp; &nbsp; }?><div class="has-sidebar">&nbsp; &nbsp; <div id="primary" class="content-area">&nbsp; &nbsp; &nbsp; &nbsp; <main id="main" class="site-main">&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; while ( have_posts() ) :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; the_post();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get_template_part( 'template-parts/content', get_post_type() );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; the_post_navigation();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // If comments are open or we have at least one comment, load up the comment template.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( comments_open() || get_comments_number() ) :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; comments_template();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; endif;&nbsp; &nbsp; &nbsp; &nbsp; endwhile; // End of the loop.&nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; </main><!-- #main -->&nbsp; &nbsp; </div><!-- #primary -->&nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; get_sidebar();&nbsp; &nbsp; &nbsp; &nbsp; get_footer();&nbsp; &nbsp; ?></div><!-- .has-sidebar -->
打开App,查看更多内容
随时随地看视频慕课网APP