Support » How-To and Troubleshooting » [Resolved] wp_get_post_categories($post->ID) – Category Names?

[Resolved] wp_get_post_categories($post->ID) – Category Names?

  • Newbie question.
    I am modifying a function, and in side of it I am trying to get a list of category titles from a post, then assign it to a variable. When I execute the code below, it only gives me a list of IDs. How do I get the category names from this array of ID’s is it possible? I’ve searched all around the codex, but I can’t find anything. Is it even possible?

    $wpcats = wp_get_post_categories($post->ID);
    $lister = implode(“,”, $wpcats);

    When I echo $lister; I get a list of category IDs, and I want a list of category names…

Viewing 4 replies - 1 through 4 (of 4 total)
  • http://codex.wordpress.org/Function_Reference/wp_get_post_categories

    wp_get_post_categories( $post->ID , array('fields' => 'names'));

    Thanks for the response, alchymyth. I really appreciate it! Unfortunately, that fix is not working to generate the category names. I am still getting a list of numeric ID’s

    I wasn’t sure that you could pass any array other than array (‘fields’ => ‘ID’). As that is defined as the default.

    Do I have to define the fields in the array before I call them, or is ‘names’ in the defaults for post categories? Better question is where would I find that information?

    I figured it out

    $wpcats = wp_get_post_categories( $post->ID );
    		$cats = array();
    		 	foreach ($wpcats as $c) {
    			$cats[] = get_cat_name( $c );
    		$lister = implode(",", $cats);

    Then I could put $lister where I needed it.

    THANK YOU DMOON79! Useful code – just what I needed!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘[Resolved] wp_get_post_categories($post->ID) – Category Names?’ is closed to new replies.