WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Category ID vanished in 2.5 - Finding ID based on slug or title (2 posts)

  1. daiko
    Member
    Posted 6 years ago #

    Hi,

    I've developed a couple of widgets that I'm now updating with increased Conditional functionality. I've included the option to specify "Post(s) in Category(-ies)". The way it works is that it allows people to identify conditonal categories based on comma separated ID(s) which in turn displays the widget in posts belonging to the category (or categories) as well as on the respective category archive pages.

    The problem is now that WordPress in 2.5 has excluded the Category ID from the list view in the admin section so that they only see slug and title. As I'm using the in_category($ID) function, I'm asking the users to put the category IDs in the input area. I'm aware that you can see the category ID through mousover on the list, but that is not user friendly.

    Is there a function where I based on category slug or title can extract the ID easily? My code looks like this at the moment:

    case "post_in_category":
    	$PiC = explode(",",$slug);
    	$InCategory = false;
    	foreach($PiC as $CategoryID) {
    	    if(is_single() && in_category($CategoryID)){
    		$InCategory = true;
                }
    	    elseif (is_category($CategoryID)) {
    		$InCategory = true;
    	    }
             }

    Never mind the $slug name (it is a variable name for ID/slug/title) and in this specific case I'm just asking for the ID.

    I guess I'll have to get an array of slugs instead and then convert them to an array of IDs in order to process using the in_category function.

    Daiko

  2. daiko
    Member
    Posted 6 years ago #

    Sorry!
    I found the answer. It does work with Category name from WP 2.5 in addition to ID. This is not updated in the documentation found here: http://codex.wordpress.org/Template_Tags/in_category

    It is described in the wordpress svn trunk:

    /*
     * in_category() - Checks whether the current post is within a particular category
     *
     * This function checks to see if the post is within the supplied category.  The category
     * can be specified by number or name and will be checked as a name first to allow for categories with numeric names.
     * Note: Prior to v2.5 of WordPress category names where not supported.
     *
     * @since 1.2.0
     *
     * @param int|string $category
     * @return bool true if the post is in the supplied category
    */

    This is good news of course and saves me from coding it myself.

Topic Closed

This topic has been closed to new replies.

About this Topic