我在Wordpress中使用高级自定义字段。我使用the_field('brand')来显示自定义字段的值,在我的例子中,它被称为brand。但是,当我使用select字段类型时,结果显示两次。如:耐克:耐克阿迪达斯:阿迪达斯
我假设一个是label,一个是value?我怎样才能只在前端显示值?这是我的代码
谢谢你,Akshay。但它仍然显示标签和值。下面是我的代码:$args = array(
'post_type' => 'kosher',
$current_query['taxonomy'] => $taxonomy_term
);
$the_query = new WP_Query( $args );
if ( have_posts() ) : while ( $the_query -> have_posts() ) : $the_query -> the_post(); ?>
<?php
$values = get_field('brand');
?>
<p><b><?php print_r($values); ?></b> <?php the_field('key'); ?> <?php the_title(); ?> <?php the_field( 'description' ); ?> </p>
<?php endwhile; else: ?>
<p>There are no posts or pages here</p>
<?php endif; ?>
下面是编辑后的代码,但仍然收到未定义的错误:
$args = array(
'post_type' => 'kosher',
$current_query['taxonomy'] => $taxonomy_term
);
$the_query = new WP_Query( $args );
if ( have_posts() ) : while ( $the_query -> have_posts() ) : $the_query -> the_post(); ?>
<?php
$field = get_field_object('brand');
$value = get_field('brand');
$label = $field['choices'][ $value ];
?>
<p><b><?php print_r($label); ?></b> <?php the_field('key'); ?> <?php the_title(); ?> <?php the_field( 'description' ); ?> </p>
<?php endwhile; else: ?>
<p>There are no posts or pages here</p>
I var_dump对象:
$field = get_field_object('brand');
var_dump($field);
下面是我得到的,值是herman jansen: herman jansen,这是每次在我的前端显示的,我应该使用什么代码来只显示"herman jansen"?
array(17) {
["key"]=> string(19) "field_533221abdb3be"
["label"]=> string(5) "Brand"
["name"]=> string(5) "brand"
["_name"]=> string(5) "brand"
["type"]=> string(6) "select"
["order_no"]=> int(3)
["instructions"]=> string(0) ""
["required"]=> int(0)
["id"]=> string(15) "acf-field-brand"
["class"]=> string(6) "select"
["conditional_logic"]=> array(3) {
["status"]=> int(0)
["rules"]=> array(1) {
[0]=> array(2) {
["field"]=> string(4) "null"
["operator"]=> string(2) "==" } }
["allorany"]=> string(3) "all" }
["choices"]=> array(4) {
["BOLS"]=> string(4) "BOLS"
["HERMAN JANSEN"]=> string(13) "HERMAN JANSEN"
["WARNICKS"]=> string(9) "WARNICK'S"
["CARLTON BLACK"]=> string(13) "CARLTON BLACK" }
["default_value"]=> string(0) ""
["allow_null"]=> int(0)
["multiple"]=> int(0)
["field_group"]=> int(140)
["value"]=> string(28) "herman jansen: HERMAN JANSEN" }
使用get_field
获取您的字段
$variable = get_field('brand');
print_r($variable);
打印$variable
,这样你就可以看到结果了。
选择字段类型请参见文档。
在文档中使用:-
$field = get_field_object('brand');
$value = get_field('brand');
$label = $field['choices'][ $value ];
,你好我注意到ACF有一些奇怪的事情。
您需要在ACF字段设置中像这样指定选择选项:-
`red : Red` instead of `red: Red`
您可能需要在:
之前添加空格。
检查ACF设置中的选项
这肯定会起作用。