SonicVoom
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.
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.
ASKER
Yes, previously and again. I don't know why it's not working.
ASKER
If I use:
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?
$category = get_the_category();
echo $category[0]->cat_name;
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);
try this
global $post;
$categories = get_the_category($post->ID
var_dump($categories);
ASKER
I've tried that too. The code dumps
array(0)
array(0)
ASKER
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here are the resources I used to put together the end result, maybe they explain it a little better that I did:
http://justintadlock.com/archives/2010/04/29/custom-post-types-in-wordpress
http://www.deluxeblogtips.com/2010/07/custom-post-type-with-categories-post.html
http://wp.tutsplus.com/tutorials/custom-post-type-pagination-chaining-method/
http://xplus3.net/2010/05/20/wp3-custom-post-type-permalinks/
http://justintadlock.com/archives/2010/04/29/custom-post-types-in-wordpress
http://www.deluxeblogtips.com/2010/07/custom-post-type-with-categories-post.html
http://wp.tutsplus.com/tutorials/custom-post-type-pagination-chaining-method/
http://xplus3.net/2010/05/20/wp3-custom-post-type-permalinks/
ASKER
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.
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.
ASKER
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['product s'] . '/%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['product s'],
340 'with_front' => false
341 ),
342 'labels' => $labels
343 ) );
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['product
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['product
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.
did you try this function?
http://codex.wordpress.org/Function_Reference/get_the_category