0

This question is probably asked before but my language not being natively english i cant really find the answer:

I got a custom post type: products and custom (hierarchy) taxonomy: collection

in the taxonomy i got for axample: - paintings (parent tax) -- classic (sub tax) -- modern (sub tax)

now i want to reach my products via: collection/paintings/classic/classic-painting-1 but i have two problems:

Problem 1: sub tax doesn't have the parent tax in the url: collection/paintings/ and then collection/classic/.. it should collection/paintings/classic/

Problem 2: if i choose a product (eg. classic-painting-1) in collection/paintings/ it goes to products/classic-painting-1 in stead of collection/paintings/classic/classic-painting-1

i hope this big question has a simple answer, thank in advance

my codes for custom post type and taxonomy:

// Custom post type    
add_action('init', 'cptui_register_my_cpt_producten');
function cptui_register_my_cpt_producten() {
register_post_type('producten', array(
'label' => 'Producten',
'description' => '',
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'capability_type' => 'post',
'map_meta_cap' => true,
'hierarchical' => true,
'rewrite' => array('slug' => 'producten', 'with_front' => true),
'query_var' => true,
'menu_position' => '1',
'supports' => array('title','editor'),
'taxonomies' => array('category','post_tag','collectie'),
'labels' => array (
  'name' => 'Producten','singular_name' => 'Product','menu_name' => 'Producten','add_new' => 'Add Product','add_new_item' => 'Add New Product','edit' => 'Edit','edit_item' => 'Edit Product','new_item' => 'New Product','view' => 'View Product','view_item' => 'View Product','search_items' => 'Search Producten','not_found' => 'No Producten Found','not_found_in_trash' => 'No Producten Found in Trash','parent' => 'Parent Product',
)
) ); }

// Custom taxonomy
add_action('init', 'cptui_register_my_taxes_collectie');
function cptui_register_my_taxes_collectie() {
register_taxonomy( 'collectie',array (
  0 => 'producten',
),
array( 'hierarchical' => true,
    'label' => 'Collecties',
    'show_ui' => true,
    'query_var' => true,
    'show_admin_column' => false,
    'labels' => array (
  'search_items' => 'Collectie',
  'popular_items' => '',
  'all_items' => '',
  'parent_item' => '',
  'parent_item_colon' => '',
  'edit_item' => '',
  'update_item' => '',
  'add_new_item' => '',
  'new_item_name' => '',
  'separate_items_with_commas' => '',
  'add_or_remove_items' => '',
  'choose_from_most_used' => '',
)
) ); 
}
2
  • Can you provide the code you use to define your post type (products) and taxonomy? Not sure if you are doing this manually in functions.php or via plugin.
    – jdm2112
    Jun 27, 2014 at 16:02
  • i've added my code
    – Gijs
    Jun 28, 2014 at 14:44

1 Answer 1

0

When asigning a post to a taxonomy, if you assign it to both a top-level and child term, the top-level will be used in the permalink. If you only select the child, I believe the parent term will be displayed as well. I agree, that this is not ideal and you may be able to work something out with wp_rewrite() but there's an easier way to get the parent term:

On the post page you'll need wp_get_post_terms(). This will return an array of terms associated with the post. If you only want the top-level term for the post, pass parent = 0 in the args array as I've done below.

$args = array( 'parent' => 0 );
$terms = wp_get_post_terms( $post->ID, 'taxonomy_name', $args );
echo '<pre>' . print_r( $terms, 1 ) . '</pre>'; // raw output

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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