0

I'm using a custom post type "portfolio" to create new posts and categories. I'm trying to retrieve the correct category/taxonomy URL from the post. I'm a couple days in on this and could really use some help. Registering post and tax below:

function portfolioposttype() {

$labels = array(
    'name'               => __('Portfolio'),
    'singular_name'      => __('Project'),
    'add_new'            => __('Add New'),
    'add_new_item'       => __('Add New Project'),
    'edit_item'          => __('Edit Project'),
    'new_item'           => __('New Project'),
    'view_item'          => __('View Project'),
    'search_items'       => __('Search Projects'),
    'not_found'          => __('No projects found'),
    'not_found_in_trash' => __('No projects found in Trash'),
    'parent_item_colon'  => __('Parent Project:'),
    'menu_name'          => __('Portfolio')
);
$args = array(
    'labels'                => $labels,
    'supports'              => array('title','editor','thumbnail'),
    'taxonomies'            => array('portfolio-categories'),
    'public'                => true,
    'show_ui'               => true,
    'show_in_menu'          => true,
    'show_in_nav_menus'     => true,
    'exclude_from_search'   => true,
    'has_archive'           => true,
    'rewrite'               => array('slug'=>'case-study'), //singe project slug
    'capability_type'       => 'post',
    'menu_position'         => 5,
    'menu_icon'             => Z_BASE_URL.'functions/assets/img/icon-portfolio.png',
    'query_var'             => true
);
register_post_type('portfolio', $args );

$taxonomy_portfolio_category_labels = array(
    'name'                       => __('Portfolio Categories'),
    'singular_name'              => __('Category'),
    'search_items'               => __('Search Categories'),
    'popular_items'              => __('Popular Categories'),
    'all_items'                  => __('All Categories'),
    'parent_item'                => __('Parent Category'),
    'parent_item_colon'          => __('Parent Category:'),
    'edit_item'                  => __('Edit Category'),
    'update_item'                => __('Update Category'),
    'add_new_item'               => __('Add New Category'),
    'new_item_name'              => __('New Category'),
    'separate_items_with_commas' => __('Separate categories with commas'),
    'add_or_remove_items'        => __('Add or remove Categories'),
    'choose_from_most_used'      => __('Choose from most used Categories'),
    'menu_name'                  => __('Categories')
);
$taxonomy_portfolio_category_args = array(
    'labels'            => $taxonomy_portfolio_category_labels,
    'public'            => true,
    'show_in_nav_menus' => true,
    'show_ui'           => true,
    'show_tagcloud'     => false,
    'hierarchical'      => true,
    'rewrite'           => array('slug'=>'projects'), //category slug
    'query_var'         => true     
);
register_taxonomy('portfolio-categories',array('portfolio'),$taxonomy_portfolio_category_args);

} 
add_action('init','portfolioposttype');

So, from www.domain.com/case-study/sample-project-1

I need to be able to link it back to the category: www.domain.com/projects/category-1

There could be another www.domain.com/case-study/sample-project-2

that would link to www.domain.com/projects/category-2

I hope this makes sense and really appreciate any advice!

2

Once you have the post's term(s) (see get_the_terms) you can use get_term_link to get the term's link:

 $url = get_term_link('category-1','portfolio-categories');

You can pass the term object, the term slug, or the term ID. If you pass the term ID it must be cast as an integer.

  • Thanks for responding! But, this is a template file and I don't know if it's category-1 or category-2 or category-45. Can I use the post-id somehow to get the "category-x" ?? – Dario Zadro Jun 14 '12 at 20:57
  • You use get_the_terms (see link in the answer) to get the terms associated with the post – Stephen Harris Jun 14 '12 at 23:13
  • Got it! Thank you.$categories = get_the_terms( $post->ID, 'portfolio-categories' ); – Dario Zadro Jun 15 '12 at 18:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.