• Hello, i need to get current post category id for styling (Even if the same post are in multiple categories i need to get only the current post category id)

    My single.php: http://pastebin.com/BDr0Vzy4

    I need to get category id on: <div class=”single-post” id=”category-id-here-goes-cat-id”>

    Any ideas?

    In index.php im getting category id like this:
    http://pastebin.com/VCwt5L2i

Viewing 1 replies (of 1 total)
  • Even if the same post are in multiple categories i need to get only the current post category id)

    that does not make sense – if a post is in multiple categories, which one do you want to choose as the current one?
    are you aware that the function post_class() already outputs CSS classes related to the single post’s categories?
    https://codex.wordpress.org/Function_Reference/post_class

    to get the category ids of all the post’s categories into the CSS class, try:

    <div class="single-post" id="<?php $categories = get_the_category();
    if($categories){
    foreach($categories as $category) {
    echo  ' category-id-' . $category->term_id;
    } ?>">

    https://codex.wordpress.org/Function_Reference/get_the_category

    what theme are you using?

Viewing 1 replies (of 1 total)

The topic ‘how to get single post category id?’ is closed to new replies.