【问题标题】:ACF if statement not working when field is empty字段为空时 ACF if 语句不起作用
【发布时间】:2020-05-05 05:20:36
【问题描述】:

我有一个名为 hero_image 的英雄图像的 ACF 字段。该字段位于我的 single.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 语句不起作用?

【问题讨论】:

  • 您是否检查过 cmets_open() 发送的内容或 get_cmets_number() 发送的内容...??如果其中任何一个为真...您的 if 将起作用...因为您使用了 OR
  • 评论已关闭,所以我不应该想象它正在发送任何内容。我已将其注释掉,结果保持不变。
  • 试试 if(trim($image)!= Null) 而不是 if($image)
  • 如果图片没有上传,$image会发送什么?
  • 如果图像尚未上传,if(trim($image)!= Null)if($image) 仍会输出 &lt;div class="hero"&gt;&lt;/div&gt;

标签: php if-statement advanced-custom-fields


【解决方案1】:

我做了一些进一步的挖掘,发现了我的问题。

我有两个 ACF 字段 hero_imagethumbnail_image。这些设置显示在所有页面和帖子上,包括自定义帖子类型。

查看header.php 文件,这是我发现的:

<?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 使用了这个变量,因为它也在循环之外。这样就解决了问题。

<?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 -->

【讨论】:

    猜你喜欢
    • 2012-07-16
    • 2021-04-23
    • 1970-01-01
    • 1970-01-01
    • 2014-01-17
    • 2014-06-19
    • 1970-01-01
    • 2023-03-13
    • 2017-10-22
    相关资源
    最近更新 更多