2016-01-20 91 views
0

我正在做一个代码,抓住一个url部分,取值,然后在列表中设置。

问题是名单

if (in_array($term->term_id, $regions)) { 
    $selected_region = 'selected'; 
} 
else { 
$selected_region = 'not'; 
} 

此代码的工作,始终未返回。

if ($term->term_id == 2) { 
$selected_region = 'varbut?'; 
} 

这项工作。

$区域是可变的,返回此:

array(2) { [0]=> string(2) "17" [1]=> string(1) "2" } 

问题出在哪里使用$ term_id?

$term_id用的var_dump返回int(17) int(2)

在其他页,单页只有一个长期工作和代码是这样的:

<?php 
    $id = get_the_ID(); 
    $postterms = wp_get_post_terms($id, 'destinations'); // get post terms 
    $parentId = $postterms[0]->term_id; // get parent term ID 
    ?> 
    <?php if (!in_array($parentId, $regions)): ?> 

为什么没有对所选功能的工作?整数和字符串值?

这里是一个足月的get代码:

$custom_terms = get_terms(array($taxonomies), $args); 
foreach($custom_terms as $term){ 
    if (in_array($term->term_id, $regions)) { 
     $selected_region = 'selected'; 
    } 
    else { 
     $selected_region = 'not'; 
    } 
    if ($term->term_id == 2) { 
     $selected_region = 'varbut?'; 
    } 
    echo $selected_region; 

    var_dump($term->term_id); 

} 

}

+0

你的问题标题说'in_array',但你的代码说'array_intersect'? –

+0

首先'var_dump($ term_id)'不能返回int(17)int(2)'它是**两个**变量。 '$ term_id'为17或'$ term_id'为2.根据if子句,它是'2'。 –

+0

Var转储在foreach上设置,如$ term_id。 – Foxsk8

回答

0

这就像你说的;

$regions : array(2) { [0]=> string(2) "17" [1]=> string(1) "2" } 
$term->term_id : int(2) 

如何将您的$地区转换为整数?