WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] wp_get_post_categories($post->ID) - Category Names? (5 posts)

  1. dmoon79
    Member
    Posted 2 years ago #

    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...

  2. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    http://codex.wordpress.org/Function_Reference/wp_get_post_categories

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

  3. dmoon79
    Member
    Posted 2 years ago #

    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?

  4. dmoon79
    Member
    Posted 2 years ago #

    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.

  5. debsch
    Member
    Posted 2 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic