亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

ACF if 語句在字段為空時不起作用

ACF if 語句在字段為空時不起作用

PHP
元芳怎么了 2022-08-05 18:59:34
我有一個用于英雄圖像的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 語句在字段未使用時不起作用?
查看完整描述

1 回答

?
紫衣仙女

TA貢獻1839條經驗 獲得超15個贊

我做了一些進一步的挖掘,發現了我的問題。


我有兩個 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_idsingle.php


我已將標題.php重命名為.然后,我通過此變量使用它,因為它也在循環之外。這已經解決了這個問題。$post_id$post_id_outside_loopsingle.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 -->


查看完整回答
反對 回復 2022-08-05
  • 1 回答
  • 0 關注
  • 118 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號