代码之家  ›  专栏  ›  技术社区  ›  Sam JvdBerg

字段为空时ACF if语句不工作

  •  0
  • Sam JvdBerg  · 技术社区  · 6 年前

    我有一个英雄形象的ACF字段,名为 hero_image . 这片土地位于我的土地的顶部 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>
    

    0 回复  |  直到 6 年前
        1
  •  0
  •   Sam JvdBerg    6 年前

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

    我有两个ACF字段 hero_image thumbnail_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_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 -->