在我的下面的代碼中,我試圖檢查$query['skill']
值是否包含在$c_skills
中。然而,它一直在失敗。當我使用我正在尋找的實際字符串時,例如"test"
然後它按預期運行。我唯一的想法也許是$query['skill']
不返回字符串,但我不能做一個var_dump
找出,因爲我使用wordpress,我正在改變代碼來實現ajax查詢。in_array函數在使用變量時不起作用?
global $user_ID;
$query = $_REQUEST['query'];
if (isset($_REQUEST['query']['skill'])) {
if (isset($query['skill']) && $query['skill'] != '') {
global $wp_query, $ae_post_factory, $post, $current_user;
$post_object = $ae_post_factory->get(PROFILE);
$profile_id = get_user_meta($user_ID, 'user_profile_id', true);
$profile = array('id' => 0, 'ID' => 0);
if($profile_id) {
$profile_post = get_post($profile_id);
if($profile_post && !is_wp_error($profile_post)){
$profile = $post_object->convert($profile_post);
}
}
$current_skills = get_the_terms($profile, 'skill');
if (!empty($current_skills)) {
$c_skills = array();
foreach ($current_skills as $key => $value) {
$c_skills[] = $value->name;
};
if(in_array($query['skill'], $c_skills)) {
$query_args['tax_query'] = array(
'skill' => array(
'taxonomy' => 'skill',
'terms' => 'test',
'field' => 'slug'
)
);
}
}
你可以提供'$ c_skills'的var_dump和'$ query ['skill']'? –
$ c_skills => [「測試」,「測試2」]和$查詢['技能']我不知道,但$查詢= $ _REQUEST ['查詢'] – Elevant