How do I Alter this wordpress php to output into an array?
<?php
$parent_cat = 57;
$child_cats = get_categories('child_of='.$parent_cat);
if($child_cats) :
echo '{ ';
foreach($child_cats as $cat) {
echo $sep . $cat->cat_name;
$sep = ', ';
}
echo ' }';
endif;
?>
The above code outputs a number of categorys in this format:
A Cut Above,A20Labs,AMCH,
how would I add ' ' around each of the elements for output like this?
'A Cut Above','A20Labs','AMCH',
2nd q开发者_如何学Cuestion, how would I code it so that that the output goes into this array code like this?
<?php $type_array = array('A Cut Above','A20Labs','AMCH',)?>
Thanks so much! Azeem
For your first question, change echo $sep . $cat->cat_name;
to echo $sep . '\''.$cat->cat_name.'\'';
This will change it to output the name with single quotes around them.
To return an array instead, try this:
<?php
$parent_cat = 57;
$child_cats = get_categories('child_of='.$parent_cat);
$type_array = array();
if($child_cats) :
foreach($child_cats as $cat) {
$type_array[] = $cat->cat_name;
}
endif;
?>
This will place the names into a new array instead of echoing them.
You can get the array you're wanting with a lot less work:
<?php
$child_cats = get_categories(array(
'child_of' => $parent_cat,
'fields' => 'names'
));
?>
精彩评论