Php 自定义字段数组不工作
我遇到了一个我似乎无法解决的非常奇怪的问题。这与我试图添加到WooCommerce结帐页面的自定义字段有关 首先,我创建了一个函数来生成数组:Php 自定义字段数组不工作,php,wordpress,woocommerce,checkout,custom-fields,Php,Wordpress,Woocommerce,Checkout,Custom Fields,我遇到了一个我似乎无法解决的非常奇怪的问题。这与我试图添加到WooCommerce结帐页面的自定义字段有关 首先,我创建了一个函数来生成数组: function eci_get_events() { $args = array( 'post_type' => 'tribe_events', 'posts_per_page' => -1, 'post_status' => 'pub
function eci_get_events() {
$args = array(
'post_type' => 'tribe_events',
'posts_per_page' => -1,
'post_status' => 'publish',
'order' => 'ASC',
'orderby' => 'meta_value_num',
'meta_key' => '_EventStartDate'
);
$event_query = new WP_Query($args);
$event_list = array();
if ($event_query->have_posts()) : while ($event_query->have_posts()) : $event_query->the_post();
$event_list[get_the_id()] = get_the_title();
endwhile; endif; wp_reset_postdata();
return $event_list;
}
然后在该函数中使用:
function ecitpm_checkout_fields( $fields ) {
$fields['billing']['eci_event'] = array(
'type' => 'select',
'label' => __('Event', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide'),
'clear' => true,
'options' => eci_get_events() // Here's that function
);
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'ecitpm_checkout_fields' );
array(2) {
[11]=>
string(11) "Hello There"
[23]=>
string(12) "Another Test"
}
然而,无论我尝试什么,当我在第一个函数中使用$event\u query
循环时,这个东西不会在前端呈现。例如:
作品:
$event_list = array();
$event_list[get_the_id()] = get_the_title();
不起作用:
编辑
根据要求,下面是由eci\u get\u events()
函数生成的数组的快速var\u dump
:
function ecitpm_checkout_fields( $fields ) {
$fields['billing']['eci_event'] = array(
'type' => 'select',
'label' => __('Event', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide'),
'clear' => true,
'options' => eci_get_events() // Here's that function
);
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'ecitpm_checkout_fields' );
array(2) {
[11]=>
string(11) "Hello There"
[23]=>
string(12) "Another Test"
}
我几乎什么都试过了,但没能找到解决办法
有没有办法解决这个问题
谢谢!
Thomas您可以尝试
var\u dump($event\u query)
查看查询结果吗?很难知道问题是否在于您的查询没有返回您认为应该返回的数据,而没有与您相同的数据。Hi@LoicTheAztec,在插件或其他地方使用次要的WP_查询
是完全可以的。另外,我刚刚安装了事件日历插件,创建了2个事件,在我的主题中添加了WooCommerce代码,效果很好。我不能重现一个问题。这里有一个屏幕截图:@helgatheviking我从未使用过事件日历插件…所以你在这方面比我有更多的专业知识…我在这方面的经验比你多10分钟。:)它只是用自己品牌的post meta创建了一些自定义的帖子类型,没有什么不同寻常的。我会尝试禁用所有其他插件并切换到默认主题。正如我所提到的,您的代码对我很有用:它检索我创建的两个事件,并生成一个带有ID/事件标题对的select下拉列表。