Php 如何在数组中列出wordpress类别id和名称?
这是我获取类别ID和名称的代码:Php 如何在数组中列出wordpress类别id和名称?,php,arrays,wordpress,Php,Arrays,Wordpress,这是我获取类别ID和名称的代码: <?php $categories = get_categories('orderby=name&hide_empty=0'); foreach ($categories as $category): $catids = $category->term_id; $catname = $category->name; endforeach; ?> 我希望该数组类似于: array( '1' => '
<?php
$categories = get_categories('orderby=name&hide_empty=0');
foreach ($categories as $category):
$catids = $category->term_id;
$catname = $category->name;
endforeach;
?>
我希望该数组类似于:
array(
'1' => 'Ctegory 1',
'2' => 'Ctegory 2',
'3' => 'Ctegory 3',
);
其中1,2,3是类别ID,Ctegory 1、Ctegory 2、Ctegory 3是类别名称
任何帮助都将不胜感激。谢谢。我想你在找这样的东西
<?php
$order_options = array('all' => 'All Categories');
$categories = get_categories('orderby=name&hide_empty=0');
foreach ($categories as $category):
$catids = $category->term_id;
$catname = $category->name;
$order_options[$catids] = $catname;
endforeach;
print_r($order_options);
希望这有帮助 我希望数组类似于:数组('1'=>'Ctegory 1','2'=>'Ctegory 2','3'=>'Ctegory 3',);其中1,2,3是类别ID,Ctegory 1,Ctegory 2,Ctegory 3是类别names@hazemhazem:是的,你将如何获得它。抱歉,如果我的问题不清楚,这是我的完整代码,我希望你能帮助它,我想列出里面的所有类别选择:@hazemhazem:所以你想要一个类别下拉列表,对吗?是的,使用$order_options=array('all'=>'all Categories',$catslug=>$catnme,)的下拉列表;我想提取$catslug中的ID和$catnme中的名称以获得最终结果:$order_options=array('all'=>'all Categories','1'=>'Ctegory 1','2'=>'Ctegory 2','3'=>'Ctegory 3',);
<?php
$order_options = array('all' => 'All Categories');
$categories = get_categories('orderby=name&hide_empty=0');
foreach ($categories as $category):
$catids = $category->term_id;
$catname = $category->name;
$order_options[$catids] = $catname;
endforeach;
print_r($order_options);
<select name="">
<?php foreach ($order_options as $cat_id => $cat_name)
{
?>
<option value="<?php echo $cat_id ?>"><?php $cat_name ?></option>
<?php } ?>
</select>