Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

hello and sorry for yet another beginner question i am extracting wordpress categories form the array but i want them to be separated by comma my code is

<?php
$args=array(
'child_of' => 79
);
$categories=get_categories($args);
foreach($categories as $category) { 
echo $category->cat_id;
?>

this will return some thing like 11223344556677

but i want the result like '11,22,33,44,55,66,77'

thank you for your help

share|improve this question

1 Answer 1

up vote 3 down vote accepted

Welcome to WPSE faq! This type of question is probably best suited for suited for stackoverflow, rather than here as the question isn't specifically related to WordPress. You just need to get an array of the IDs (a simple foreach loop could do this - or use the Wordpress' wp_list_pluck) and then explode that array:

//$categories is the array of category objects
$cat_ids = wp_list_pluck($categories,'term_id');
echo "'".implode(',',$cat_ids)."'";

Edit

And to incorporate this method into the OP's code example:

<?php
$args=array(
'child_of' => 79
);
$categories=get_categories($args);
foreach($categories as $category) { 
    $cat_ids[] = $category->term_id;
}
echo "'".implode(',',$cat_ids)."'";
?>
share|improve this answer
    
+1 to this being a PHP question, rather than a WordPress question. –  Chip Bennett Feb 23 '12 at 17:36
    
thank you both of you, @stephen, i will keep in mind regarding the posting. however Chip solution returned me with '""""' while Stephen was perfect on target –  faq Feb 23 '12 at 17:47
    
Glad to have helped. The discrepancy between the solutions was because Chip used $category->cat_id rather than $category->term_id. I've edited this. –  Stephen Harris Feb 23 '12 at 17:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.