【问题标题】:Variable returns boolean false w/var_dump()变量返回布尔值 false w/var_dump()
【发布时间】:2020-07-20 16:55:45
【问题描述】:

我正在尝试使用从自定义字段中提取的变量来输入 tax_query 参数。然而什么都没有返回。我对我的变量做了一个 var_dump,它一直返回“boolean:False”。所以我想也许我需要运行一个循环(我的自定义字段是一个组的子字段),然后 var_dump 什么也不返回。我对 WP/PHP 开发相当陌生,不确定这里发生了什么。任何帮助将不胜感激!

<?php
if(have_rows('wine_profile')) : 
    while(have_rows('wine_profile')) : the_row();

$label = get_sub_field("taxonomy_label");
var_dump($label);

$wineProfiles = new WP_Query(array(

    'posts_per_page' => '-1',
    'post_type'   => 'wines',
    'tax_query' => array(
        array(
            'taxonomy'  => 'labels',
            'field'     => 'slug',
            'terms'     => $label
        )
    ),

    'order' => 'DESC',
    'orderby' => 'ID'

));
if ($wineProfiles->have_posts()) : while ($wineProfiles->have_posts()) : $wineProfiles->the_post();
        get_template_part('includes/component', 'colorBlockLg');
        get_template_part('includes/component', 'profile');
?>

<?php endwhile;
endif; endwhile; endif; ?>

【问题讨论】:

  • 如果它返回 false,这意味着它没有找到任何东西。确保“taxonomy_label”是正确的名称。您也可以查看 var_dump('wine_profile') 以了解其中的内容。
  • 感谢卢克的回复。 'wine_profile' 是一个自定义字段组,所以我尝试了 $wine = get_field('wine_profile') 、 var_dump($wine) 并从中获取 'null'。
  • 这很令人困惑,因为在 wp 管理员中我可以看到这些值,但它们没有被返回。

标签: php wordpress advanced-custom-fields custom-post-type


【解决方案1】:

尝试使用-

$label = get_sub_field("taxonomy_label",get_the_ID());
var_dump($label);

【讨论】:

    【解决方案2】:

    我猜是因为你还没有开始后循环!

    <?php 
    if ( have_posts() ) {
        while ( have_posts() ) {
            the_post(); 
            //
            // Put your code here       //
        } // end while
    } // end if
    ?>
    ``
    

    【讨论】:

    • 是的,我也尝试过这样做。该字段是组的一部分,因此我使用了“有行”循环,但是对于这两种类型的循环都没有显示任何内容。
    猜你喜欢
    • 2010-11-30
    • 1970-01-01
    • 2017-11-23
    • 1970-01-01
    • 2017-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多