在模板文件中显示分类名称

时间:2014-11-14 06:31:48

标签: php wordpress

我有single-property.php模板文件。我想在该模板文件中显示分类名称和值。这是我在模板文件中的代码......

 $prop_category_array            =   get_the_terms($post->ID, 'property_category');

 if(isset($prop_category_array[0])){
     $prop_category_selected   =   $prop_category_array[0]->term_id;
 }

 $prop_action_category_array     =   get_the_terms($post->ID, 'property_action_category');
 if(isset($prop_action_category_array[0])){
    $prop_action_category_selected           =   $prop_action_category_array[0]->term_id;
 } 

我想在$prop_category_selected$prop_action_category_selected中显示值。 这是第一个数组的输出

Array ( [5] => stdClass Object ( [term_id] => 5 [name] => Agriculture Lands [slug] =>  
 agriculture-lands [term_group] => 0 [term_taxonomy_id] => 5 [taxonomy] => property_category
[description] => Houses [parent] => 0 [count] => 9 [object_id] => 6915 [filter] => raw ) )

这是第二个数组的输出

Array ( [150] => stdClass Object ( [term_id] => 150 [name] => Sales [slug] => sales [term_group] 
 => 0 [term_taxonomy_id] => 151 [taxonomy] => property_action_category [description] => Sales
 [parent] => 0 [count] => 9 [object_id] => 6915 [filter] => raw )

2 个答案:

答案 0 :(得分:0)

试试这个

$prop_category_array            =   get_the_terms($post->ID, 'property_category');

 if(is_object($prop_category_array[5])){
     $prop_category_selected   =   $prop_category_array[5]->term_id;
 }

 $prop_action_category_array     =   get_the_terms($post->ID, 'property_action_category');
 if(is_object($prop_action_category_array[150])){
    $prop_action_category_selected           =   $prop_action_category_array[150]->term_id;
 } 

 echo isset($prop_action_category_selected)?$prop_action_category_selected:'No value for variable  $prop_action_category_selected';
 echo isset($prop_category_selected)?$prop_category_selected:'No value for variable $prop_category_selected ';

我不确定这是你预期的答案,你说未定义的变量错误,所以我写这个。只需检查一次。

答案 1 :(得分:0)

这是我的工作代码......

$property_category= strip_tags( get_the_term_list( $wp_query->post->ID, 'property_category', '', ', 
 ', '' ) );
$property_action_category= strip_tags( get_the_term_list( $wp_query->post->ID,
'property_action_category', '', ', ', '' ) );

echo $property_category." and ".$property_action_category;