• Aloha,

    I’m trying to make a dynamic sidebar menu that changes depending on the category of the current post.

    Question 1:
    My problem is that can’t get the category ID for the current post. I have been able to get all the category IDs, etc., but what I need is the one and only category ID for the current post.

    Question 2:
    Am I correct when I assume that it’s impossible to assign a category to a page?

    Thanks, phimix

Viewing 2 replies - 1 through 2 (of 2 total)
  • I have created this code with a good helpful friend :

    function get_the_category_id($postID) {
    global $wpdb;
    $sql = “SELECT category_id FROM $wpdb->post2cat WHERE post_id = $postID”;
    $result = $wpdb->get_var($sql);
    return $result;
    }

    Hope it helps 🙂

    Thread Starter phimix

    (@phimix)

    Great, thanks!

    It turned out I needed a slightly different function, but with the help of the code above I was able to throw something together and put it in category.php

    function is_in_category($postID, $categoryID) {
    global $wpdb;
    $sql = “SELECT COUNT(post_id) FROM $wpdb->post2cat WHERE post_id = $postID AND category_id = $categoryID”;
    $result = $wpdb->get_var($sql);

    if($result > 0)
    {
    return 1;
    }
    else
    {
    return 0;
    }
    }

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Get the category id’ is closed to new replies.