WordPress.org

Forums

Category ID (5 posts)

  1. roxer82
    Member
    Posted 7 years ago #

    Hello,
    I need to get the category ID of each post. I was using this code and it used to work:

    $postid=$post->ID;
    $categoriapost=$wpdb->get_var("SELECT term_taxonomy_id FROM wp_term_relationships WHERE object_id=$postid order by term_taxonomy_id asc limit 0,1");}
    $categoriacat = get_query_var('cat');

    Bue now it isn't working, is there any way to get it in this last version of WP?
    Thanks!

  2. Sridhar Katakam
    Member
    Posted 7 years ago #

  3. roxer82
    Member
    Posted 7 years ago #

    Thanks for your answer srikat but it shows the last post of each category, I just need to show the category ID of a post

  4. SimonJ
    Member
    Posted 7 years ago #

    To display the cat_ID for each posts in your loop :

    <?php foreach((get_the_category()) as $cat) { echo $cat->cat_ID . ' '; } ?>

    http://codex.wordpress.org/Template_Tags/get_the_category

    S.

  5. roxer82
    Member
    Posted 7 years ago #

    Thanks SimonJ! :D

Topic Closed

This topic has been closed to new replies.

About this Topic