首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ACF if语句在字段为空时不起作用

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

Stack Overflow用户
提问于 2020-01-19 00:00:22
回答 1查看 79关注 0票数 0

我有一个名为hero_image的英雄图像的ACF字段。该字段位于我的single.php页面的顶部,如下所示:

代码语言:javascript
复制
<?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变量从循环外部获取字段。图像将按预期加载。

如果一个图片还没有上传到使用字段的帖子中,我希望在前端不会有标记。然而,我仍然看到以下几点:

代码语言:javascript
复制
<div class="hero"></div>

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-20 18:22:58

我做了进一步的调查,发现了我的问题。

我有两个ACF字段hero_imagethumbnail_image。它们被设置为出现在所有页面和帖子上,包括自定义帖子类型。

查看header.php文件,我发现了以下内容:

代码语言:javascript
复制
<?php
    // Get post ID
    $post_id = get_queried_object_id();
    // Hero image
    $hero = get_field('hero_image', $post_id);
    $hero_url = wp_get_attachment_url( get_field('hero_image', $post_id), 'hero');
?>

<?php if ( is_single() || is_archive() ): ?>
    <header id="masthead" class="site-header">
<?php else: ?>
    <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_id变量通过single.php工作。

我已经将header.php中的$post_id重命名为$post_id_outside_loop。然后我通过single.php使用了这个变量,因为它也在循环之外。这就解决了这个问题。

代码语言:javascript
复制
<?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_outside_loop is set via header.php
    $image = get_field('hero_image', $post_id_outside_loop);
    if ($image) {
        echo '<div class="hero">'.wp_get_attachment_image( $post_id_outside_loop, '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();

            // If comments are open or we have at least one comment, load up the comment template.
            if ( comments_open() || get_comments_number() ) :
                comments_template();
            endif;

        endwhile; // End of the loop.
        ?>

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

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

    <?php
        get_sidebar();
        get_footer();
    ?>

</div><!-- .has-sidebar -->
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59802280

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档