January 8, 201115 yr Does anyone know how I can get my category names separated by comma's? This is the code I have: <?php wp_list_categories('orderby=name&title_li=Select A Category:'); ?> and CSS I have makes it display inline, so I want to have a comma+space after each category name. Thanks E
January 10, 201115 yr I don't know if this will help you, I haven't tried it but from the single comment it works with one issue, which is placing a comma after the last one too. http://www.walkernews.net/2010/01/09/display-wordpress-categories-in-one-comma-separated-line/ Edited January 10, 201115 yr by Dizi
January 11, 201115 yr Author thanks for that - I'll give it a go - having a bit of time off due to RSI - been overworking to get my re-design done and suffering the consequences!
March 5, 201115 yr Author I've still not found a solution to this - anyone got any ideas? Dizi your option just creates links within a ul - it takes the li's out so you get something like this: <ul> <a href="">category</a>, <a href="">category</a>, <a href="">category</a>, </ul> and there's a comma at the end which isn't great but I could live with that if I could just get comma's in between the categories. At the moment I'm just putting a physical comma at the end of the category name within Wordpress which isn't an ideal solution either, but it works for now. Anyone know how I can do this with PHP?
March 5, 201115 yr If you want them as links, try: <?php echo get_the_term_list( $post->ID, 'category', '', ', ', '' ); ?> otherwise: <?php get_the_terms($post->ID, 'category'); ?> will return an array. Edit: should probably mention, these need to be within The Loop. Edited March 5, 201115 yr by Renaissance-Design
March 6, 201115 yr Author thanks - but neither of these work for whether inside or outside the loop - only brings back one category - I need this to work outside the loop so will keep looking for another solution. thanks.
March 6, 201115 yr Author if I use this outside the loop: <?php wp_list_categories( $post->ID, 'category', '', ', ', '' ); ?> It churns out the categories, but no comma's appear between them.
March 6, 201115 yr Wups, my bad. Sorry; I was dead on my feet yesterday. Only the second one must be used in The Loop, you're absolutely right. Here's a new template function just for you: <?php function rd_list_cats() { $cats = get_categories(); foreach($cats as $cat) { $catsArray[] = '<a href="' . get_category_link($cat->cat_ID) . '" title="' . $cat->description . '">' . $cat->category_nicename . '</a>'; } echo implode(', ', $catsArray); } ?> Edited March 6, 201115 yr by Renaissance-Design
March 8, 201115 yr Author I've plonked this into my index.php file and it generates an error and doesn't load the page - so do I put this in the functions.php file instead? if so what do I put on my index.php file? I've tried it with: <?php wp_list_categories('orderby=name&title_li=Select A Category:'); ?> and <?php wp_list_categories( $post->ID, 'category', '', ', ', '' ); ?> but no luck.
March 8, 201115 yr Add this to your functions.php: function rd_list_cats() { $cats = get_categories(); foreach($cats as $cat) { $catsArray[] = '<a href="' . get_category_link($cat->cat_ID) . '" title="' . $cat->description . '">' . $cat->category_nicename . '</a>'; } echo implode(', ', $catsArray); } and this wherever you want to call it in your index.php: <?php rd_list_cats(); ?>
March 8, 201115 yr Author right I think we're getting somewhere now - it pulls in categories and these are comma separated, but it's not pulling in the category name. So instead of 'Web Design' it says 'web-design' and instead of 'CMS' it's pulling in 'content-management-systems' - is there a way to solve this? Also nothing is getting pulled into the title tag: a href="http://www.eskymo.co.uk/category/content-management-systems/" title="">content-management-systems</a> it's also not a list - just link after link - what I had before with <?php wp_list_categories('orderby=name&title_li=Select A Category:'); ?> created a list. Am I asking for too much - maybe it's not possible.
March 8, 201115 yr I could've sworn I posted a follow-up here earlier. *shrug* Must be getting old. I set the title field to be the category description - if you haven't filled in a description in yoursite.com/wp-admin/edit-tags.php?taxonomy=category then it won't show anything. Maybe a better way of writing it would have been: function rd_list_cats() { $cats = get_categories(); foreach($cats as $cat) { $description = ($cat->description != '') ? ' title="' . $cat->description . '"' : ''; $catsArray[] = '<a href="' . get_category_link($cat->cat_ID) . $description . '">' . $cat->name . '</a>'; } echo implode(', ', $catsArray); } If you're desperate to get a list, put the following in the desired place on the page and give me a link to it: <?php echo '<!--' . var_dump($post) . '-->'; echo get_the_term_list( $post->ID, 'category', '', ', ', '' ); ?>
March 9, 201115 yr Author Using: function rd_list_cats() { $cats = get_categories(); foreach($cats as $cat) { $description = ($cat->description != '') ? ' title="' . $cat->description . '"' : ''; $catsArray[] = '<a href="' . get_category_link($cat->cat_ID) . $description . '">' . $cat->name . '</a>'; } echo implode(', ', $catsArray); } with: <?php echo get_the_term_list( $post->ID, 'category', '', ', ', '' ); ?> gives me comma separated categories, but on the index page it's not churning out ALL the categories that have posts associated with them it just show CMS and Web Design when it's should show up Blogs, Wordpress as well ans many more. If I then go to a single post it only shows the categories related to that post and not ALL the categories up for selection. I don't understand the PHP so can't tweak. Site in maintenance mode, so can't send you a link.
March 9, 201115 yr All categories in list: <?php wp_list_categories('hide_empty=0'); ?> All non-empty categories in list: <?php wp_list_categories(); ?> All categories, comma-separated links: <?php rd_list_cats(false); ?> All non-empty categories, comma-separated links: <?php rd_list_cats(); ?> Using the following in functions.php: function rd_list_cats($hide_empty = true) { $cats = ($hide_empty == true) ? get_categories() : get_categories('hide_empty=0'); foreach($cats as $cat) { $description = ($cat->description != '') ? ' title="' . $cat->description . '"' : ''; $catsArray[] = '<a href="' . get_category_link($cat->cat_ID) . $description . '">' . $cat->name . '</a>'; } echo implode(', ', $catsArray); }
March 9, 201115 yr Author Thank you so so much - this works perfectly! I really appreciate you sticking out with this and helping me out as I'm going to use this code over and over on lots of sites. If you ever need any help with design I'd be happy to lend a hand.
Create an account or sign in to comment