Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

wordpress: get category slug from post ID

Posted on 2011-09-28
13
Medium Priority
?
2,917 Views
Last Modified: 2012-05-12
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.
0
Comment
Question by:SonicVoom
  • 6
  • 3
  • 2
  • +1
12 Comments
 
LVL 11

Expert Comment

by:Paolo Santiangeli
ID: 36720707
0
 
LVL 2

Author Comment

by:SonicVoom
ID: 36720777
Yes, previously and again. I don't know why it's not working.
0
 
LVL 2

Author Comment

by:SonicVoom
ID: 36742886
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?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 11

Expert Comment

by:Paolo Santiangeli
ID: 36754335
maybe you are outside a th loop?

try this
global $post;
$categories = get_the_category($post->ID);
var_dump($categories);
0
 
LVL 2

Author Comment

by:SonicVoom
ID: 36761956
I've tried that too. The code dumps

array(0)
0
 
LVL 2

Author Comment

by:SonicVoom
ID: 36762792
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.

0
 
LVL 23

Accepted Solution

by:
jeremyjared74 earned 2000 total points
ID: 36789582
OK, here is how I fixed a similar it. I tried to set up a similar issue to test it on. I've just stated messing with custom post types and taxonomies so I had to try a few things before posting. I was having issues getting the slugs to work for categories that were hierarchical. I tried several online suggestions with no luck. So I took all the options available and put them into one big custom post type function. I whittled it down a bit, but it might still have a few more options that what you need.

Here is the code I worked out. (I'm not using the e-commerce plug-in, so it might not turn out the same):

<?php 
register_post_type( 
  'city', array(
  'label' => 'Cities',
  'singular_label' => 'City',
  'description' => 'A bug to fix, task to complete, or anything else that need to be done',
  'public' => TRUE,
  'publicly_queryable' => TRUE,
  'show_ui' => TRUE,
  'query_var' => TRUE,
  'show_in_menu' => TRUE, 
  'rewrite' => TRUE,
  'capability_type' => 'post',
  'hierarchical' => TRUE,
  'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments', 'custom-fields', 'revisions'),
  'menu_position' => 5,
  'rewrite' => array(
  'slug' => 'city',
  'with_front' => TRUE, ),
  ));
register_taxonomy (
  'city_state', 'city', array(  // this is IMPORTANT
  'label' => 'States',
  'singular_label' => 'State',
  'public' => TRUE,
  'show_tagcloud' => TRUE,
  'hierarchical' => TRUE,
  'query_var' => TRUE,
  'show_tagcloud' => TRUE,
  'rewrite' => array(
  'slug' => 'state' ),
  'taxonomies' => array('category', 'post_tag')
  ));
?>

Open in new window


The code above ends up doing the following:

The City is the custom post type, but it can also be added as a post by adding the city name in the new custom post type section.

The State is a category, which allows you to set up the states to fall into those categories. If you simply added a city without a state the slug would only show the city, if only a state then it only shows the state. But if you add a city and add it to a state category then it gets amended to the post or page slug.

The way it shows for me:
Say my site URL is http://something.com/

I add a state, it becomes:
Say my site URL is http://something.com/state (<--name of the state)

If I add a city and assign it to that category in the custom post editor it would then look like this:
Say my site URL is http://something.com/state/city (<--name of the city)

The piece of code that does the magic is this:
register_taxonomy (
  'city_state', 'city', array(

Open in new window


It should work on about any combination you might use for setting up your categories (which are actually taxonomies), with the custom post type (city).

I hope I didn't loose you, I understand if you need some additional help (it's clear as mud!).

Regards,
JeremyJared74
0
 
LVL 2

Author Comment

by:SonicVoom
ID: 36817017
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.
0
 
LVL 23

Expert Comment

by:jeremyjared74
ID: 36817485
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.
0
 
LVL 2

Author Comment

by:SonicVoom
ID: 36817677
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      ) );
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 37343808
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
The purpose of this video is to demonstrate how to add AdSense Ads to a WordPress Website, and how to set up WordPress to automatically place Ads in Sidebars. This will be demonstrated using a Windows 8 PC. Log into your AdSense account. : Cli…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses
Course of the Month9 days, 10 hours left to enroll

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question