Link to home
Start Free TrialLog in
Avatar of SonicVoom
SonicVoomFlag for United States of America

asked on

wordpress: get category slug from post ID

I need to add category slugs as post classes. I'm using wp-e-commerce, so these are actually product custom post types, and the custom functions used by the plugin seem to be breaking all of the solutions I've researched so far.

I have the product ID/post ID, so I need a line of code that i can put the ID into, and get back the category slug(s) for that ID.
Avatar of Paolo Santiangeli
Paolo Santiangeli
Flag of Italy image

Avatar of SonicVoom

ASKER

Yes, previously and again. I don't know why it's not working.
If I use:
$category = get_the_category(); 
	echo $category[0]->cat_name;

Open in new window


WP_Debug says:
Undefined offset: 0
Trying to get property of non-object

I get the same thing if I pass an ID into get_the_category.

Am I misinterpreting how custom post types work?
maybe you are outside a th loop?

try this
global $post;
$categories = get_the_category($post->ID);
var_dump($categories);
I've tried that too. The code dumps

array(0)
This is happening inside of The Loop.

The wp-e-commerce plugin then operates inside of the the Page, and has its own loop for the product custom post type.

ASKER CERTIFIED SOLUTION
Avatar of jeremyjared74
jeremyjared74
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Wow. If I can really take in all that, and it's been a bit helpful already, I will likely understand enough to answer my own question.

While I chew on that, here's a recap:

I'm using wp-e-commerce. Products are custom post type 'wpsc-product'. They are categorized in a custom taxonomy 'wpsc_product_category'. It has a slug defined.

I know the product ID, and need the slugs of the categories that it is in.
Can you post your code for the custom post type and taxonomy? It might give me a better understanding or what needs to be changed.
The source is here:
http://supersonic.lt/wpec/nav.html?wpsc-core/wpsc-functions.php.source.html

here's the post type

 265          $labels = array(
 266              'name' => _x( 'Products', 'post type name', 'wpsc' ),
 267              'singular_name' => _x( 'Product', 'post type singular name', 'wpsc' ),
 268              'add_new' => _x( 'Add New', 'admin menu: add new product', 'wpsc' ),
 269              'add_new_item' => __('Add New Product', 'wpsc' ),
 270              'edit_item' => __('Edit Product', 'wpsc' ),
 271              'new_item' => __('New Product', 'wpsc' ),
 272              'view_item' => __('View Product', 'wpsc' ),
 273              'search_items' => __('Search Products', 'wpsc' ),
 274              'not_found' =>  __('No products found', 'wpsc' ),
 275              'not_found_in_trash' => __( 'No products found in Trash', 'wpsc' ),
 276              'parent_item_colon' => '',
 277              'menu_name' => __( 'Products', 'wpsc' )
 278            );
 279      // Products
 280      register_post_type( 'wpsc-product', array(
 281          'capability_type' => 'post',
 282          'hierarchical' => true,
 283          'exclude_from_search' => false,
 284          'public' => true,
 285          'show_ui' => true,
 286          'show_in_nav_menus' => true,
 287                  'menu_icon' => WPSC_CORE_IMAGES_URL . "/credit_cards.png",
 288          'labels' => $labels,
 289          'query_var' => true,
 290          'register_meta_box_cb' => 'wpsc_meta_boxes',
 291          'rewrite' => array(
 292              'slug' => $wpsc_page_titles['products'] . '/%wpsc_product_category%',
 293              'with_front' => false
 294          )
 295      ) );

here's the taxonomy:

 323      // Product categories, is heirarchical and can use permalinks
 324      $labels = array(
 325          'name' => _x( 'Categories', 'taxonomy general name', 'wpsc' ),
 326          'singular_name' => _x( 'Product Category', 'taxonomy singular name', 'wpsc' ),
 327          'search_items' => __( 'Search Product Categories', 'wpsc' ),
 328          'all_items' => __( 'All Product Categories', 'wpsc' ),
 329          'parent_item' => __( 'Parent Product Category', 'wpsc' ),
 330          'parent_item_colon' => __( 'Parent Product Category:', 'wpsc' ),
 331          'edit_item' => __( 'Edit Product Category', 'wpsc' ),
 332          'update_item' => __( 'Update Product Category', 'wpsc' ),
 333          'add_new_item' => __( 'Add New Product Category', 'wpsc' ),
 334          'new_item_name' => __( 'New Product Category Name', 'wpsc' )
 335      );
 336      register_taxonomy( 'wpsc_product_category', 'wpsc-product', array(
 337          'hierarchical' => true,
 338          'rewrite' => array(
 339              'slug' => $wpsc_page_titles['products'],
 340              'with_front' => false
 341          ),
 342              'labels' => $labels
 343      ) );
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.