• Resolved Begin

    (@bentalgad)


    I want to get the current post’s categories (most of my posts
    are in multiple categories) as simple text, separated by commas
    like that: movies, articles, photos, but when the post is only
    in one category I want only this category’s name with no comma
    like that: movies

    I would really appreciate if someone knows how to that because
    I tried playing with the wp_list_categories and get_categories but
    could not get it right…

    Thank’s!

Viewing 13 replies - 1 through 13 (of 13 total)
  • Something like <?php the_category(',') ?> should work.

    Thread Starter Begin

    (@bentalgad)

    yes that works great for generating links, but I need it as a simple text… ??

    Thank’s!

    <?php $my_cats = get_categories(',');
    echo $my_cats;?>
    Thread Starter Begin

    (@bentalgad)

    it’s just displaying the word “array” :-<

    My bad. Try:

    <?php $my_cats = get_categories();
    $num = count($my_cats);
    $c = 0;
    foreach( $my_cats as $my_cat ) {
    	$c++;
    	echo $my_cat;
    	if( $c < $num ) echo ', ';
    }?>

    Warning: Off the top of my head so untested.

    Thread Starter Begin

    (@bentalgad)

    Wow that’s a complicated one… but it’s saying:
    “catchable fatal error: object of class stdClass could not be converted to string”
    ???!!!!!???? ??

    Thread Starter Begin

    (@bentalgad)

    it’s kind of weird that wordpress gives so much amazing complicated
    stuff in a simple code structure but some of the simplest things
    don’t have any short code at all…

    I missed the closing ?> at the end initially. Have you added that?

    Thread Starter Begin

    (@bentalgad)

    ya sure ??

    Aha! Wrong function! Your mention of wp_list_categories threw me.

    <?php $my_cats = get_the_category();
    $num = count($my_cats);
    $c = 0;
    foreach( $my_cats as $my_cat ) {
    	$c++;
    	echo $my_cat->category_nicename;
    	if( $c < $num ) echo ', ';
    }?>

    And I’ve tested this one. ??

    Thread Starter Begin

    (@bentalgad)

    I think it works, but maybe doesn’t work well with the hebrew language…
    because it’s generating some “7%91%d7%a4%d7%aa%d7%97-%d7%aa%d7%a7%d7%9…”
    gibberish…

    Hmmm… that looks like an encoding problem. Try using cat_name instead of category_nicename.

    Thread Starter Begin

    (@bentalgad)

    Yaaaa!!! right!!!! that works great!!! Thank you very much esmi!

    :-)))

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘get post's categories as simple text’ is closed to new replies.