如何查找第一个字符是否为下划线?

时间:2018-11-05 04:36:42

标签: php wordpress

我尝试过

foreach ($categories as $category) {
  if( $category[0] === '_'  ) {
    $option .= '<option>'.$category->name.'</option>';
  }
}

但是给我

  

致命错误:不能将WP_Term类型的对象用作数组

我尝试使用implode()

foreach ($categories as $category) {
  $string = implode($category);
  if( $category[0] === '_'  ) {
    $option .= '<option>'.$category->name.'</option>';
  }
}

但是给我

  

警告:implode():参数必须是数组

     

致命错误:不能将WP_Term类型的对象用作数组

更新

var_dump($categories);


array(1) { 
    [5]=> object(WP_Term)#999 (16) {
        ["term_id"]=> int(1) 
        ["name"]=> string(13) "Uncategorized" 
        ["slug"]=> string(13) 
        "uncategorized" 
        ["term_group"]=> int(0) 
        ["term_taxonomy_id"]=> int(1) 
        ["taxonomy"]=> string(8) "category" 
        ["description"]=> string(0) "" 
        ["parent"]=> int(0) 
        ["count"]=> int(9) 
        ["filter"]=> string(3) "raw" 
        ["cat_ID"]=> int(1) 
        ["category_count"]=> int(9) 
        ["category_description"]=> string(0) "" 
        ["cat_name"]=> string(13) "Uncategorized" 
        ["category_nicename"]=> string(13) "uncategorized" 
        ["category_parent"]=> int(0) 
    } 
}

1 个答案:

答案 0 :(得分:0)

尝试一下

foreach ($categories as $category) {
  if($category->name[0] == '_'){
    $option .= '<option>'.$category->name.'</option>';
  }
}