get_field() ACF wordpress返回null取决于我声明变量的顺序



我使用ACF Pro在自定义帖子类型中创建了两个选择。我用wordpress循环做了一个存档来显示这些元素但根据我声明的顺序,我的变量返回null。另外,如果我把它放在the_title()之前,在wp_loop应该返回post类型的标题的情况下,它返回页面的标题。

$stagesId = get_field("stages");
$jobsId = get_field("job");
var_dump($stagesId);
var_dump($jobsId);

在这种情况下,它显示:3和NULL

$jobsId = get_field("job");
$stagesId = get_field("stages");
var_dump($jobsId);
var_dump($stagesId);

在这种情况下它显示:21和NULL

$args = array(
'post_type'      => 'catalogue',
'post_status' => 'publish',
'posts_per_page' => -1,
'order'          => 'DESC',
'orderby'        => 'post_date'
);
$the_query = new WP_query($args);
$jobs = array(); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php 
$jobs[$the_query->post->post_title] = $the_query->post->post_title;
?>
<a href="#contact_form?<?php echo get_the_title()  ?>" class="card--link">
<div class="card">
<div class="card--header">
<h3><?php the_title() ?></h3>
</div>
<div class="card--body">
<p><?php the_field("content") ?></p>
<?php
$stagesId = get_field("stages");
$jobsId = get_field("job");
var_dump($jobsId);
var_dump($stagesId);
<p>Nombre de personne disponible pour ces jobs : <?php echo $count ?></p>
</div>
</div>
</a>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; 
下面是我创建字段 的方法
array(
'key' => 'field_61386dc51b308',
'label' => 'Job',
'name' => 'job',
'type' => 'select',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'choices' => array(
),
'default_value' => array(
),
'allow_null' => 0,
'multiple' => 1,
'ui' => 0,
'return_format' => 'value',
'ajax' => 1,
'placeholder' => '',
),
array(
'key' => 'field_613f0c5df7a97',
'label' => 'Stages',
'name' => 'stages',
'type' => 'select',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'choices' => array(
),
'default_value' => array(
),
'allow_null' => 0,
'multiple' => 1,
'ui' => 0,
'return_format' => 'value',
'ajax' => 1,
'placeholder' => '',
),

在ACF文档中有几个需要记住的设置。您希望同时存储标签和值还是其中之一。我们有默认值吗?我们允许null吗?

从文档中允许Null选项-如果选中,选择列表将以标记为"- select -"的空选项开始。如果使用Stylized UI设置,这个选项将被一个' x '图标取代,允许您删除所选的值。

下面是一个带有select field 的post循环示例
get_header();
if(have_posts()):
while(have_posts()): the_post();
// If select is array ( value and label)
$color = get_field('color');
if(isset($color['value'])):
var_dump($color['value'].' '.$color['label']);
endif;
// If select is label or value
$color = get_field('color');
if(isset($color)):
var_dump($color);
endif;
endwhile;
endif;
get_footer(); 

对于multiselect字段,我们需要循环我们的值。

get_header();
if(have_posts()):
while(have_posts()): the_post();
echo '<div>';
the_title();
echo '</br>';
$colors = get_field('colors');
if(isset($colors)):
echo implode(',',$colors); // or use foreach
foreach($colors as $color):
echo $color;
endforeach;
endif;
echo '</br>';
$sizes = get_field('sizes');
if(isset($sizes)):
echo implode(',',$sizes); // or use foreach
foreach($sizes as $size):
echo $size;
endforeach;
endif;
echo '</div>';
endwhile;
endif;
get_footer(); 

最新更新