2 replaced http://wordpress.stackexchange.com/ with https://wordpress.stackexchange.com/
source | link

After following Jeffs suggestion on this threadthis thread to create nested parent and child taxonomy terms within a permalink like...

Jeff, who helped me make it this far from this threadthis thread, said the following...

After following Jeffs suggestion on this thread to create nested parent and child taxonomy terms within a permalink like...

Jeff, who helped me make it this far from this thread, said the following...

After following Jeffs suggestion on this thread to create nested parent and child taxonomy terms within a permalink like...

Jeff, who helped me make it this far from this thread, said the following...

1
source | link

How to check if last uri segment is a custom post type or taxonomy term?

After following Jeffs suggestion on this thread to create nested parent and child taxonomy terms within a permalink like...

url.com/custom-post-type/taxonomy-parent/taxonomy-child/cpt-entry

It mostly works except for one problem.

Before I continue, I should mention I have a custom post type called gallery and a taxonomy called gallery_cat.


WORKING

/gallery/ - archive-gallery.php
/gallery/wedding-cakes/ - parent taxonomy - taxonomy-gallery_cat.php     
/gallery/wedding-cakes/chocolate/ - child taxonomy - taxonomy-gallery_cat.php      
/gallery/wedding-cakes/chocolate/cpt-entry/ - CPT entry under child taxonomy - single-gallery.php

NOT WORKING

/gallery/wedding-cakes/cpt-entry/ - CPT entry under parent taxonomy


Here is my code in functions.php:

// ADD A GALLERY CUSTOM POST TYPE WITH GALLERY_CAT TAXONOMY
add_action('init', 'create_gallery_post_type');

function create_gallery_post_type() {

register_taxonomy(
    'gallery_cat',
    'gallery',
     array(
        'label' => 'Categories',
        'singular_label' => 'Category',
        'hierarchical' => true,
        'query_var' => true,
        'rewrite' => array('slug' => 'gallery', 'hierarchical' => true ),
    )
);

$labels = array(
    'name' => _x('Gallery', 'post type general name'),
    'singular_name' => _x('Photo', 'post type singular name'),
'add_new' => _x('Add New', 'gallery'),
);

$args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'query_var' => true,
    'capability_type' => 'post',
    'hierarchical' => false,
    'menu_position' => null,
    'supports' => array('title','editor','thumbnail', 'excerpt', 'author', 'comments'),
    'rewrite' => array(
        'slug' => 'gallery/%gallery_cat%',
        'with_front' => false
    ),
    'has_archive' => 'gallery',
    'menu_icon' => get_bloginfo('template_directory') . '/images/gallery-icon.png' //16x16 png if you want an icon
); 

register_post_type( 'gallery' , $args );
}



function filter_post_type_link($link, $post) {
if ($post->post_type != 'gallery')
    return $link;
if ($cats = get_the_terms($post->ID, 'gallery_cat')) {
    $link = str_replace('%gallery_cat%', rtrim(get_taxonomy_parents(array_pop($cats)->term_id, 'gallery_cat', false, '/', true),"/"), $link); // see custom function defined below
}
return $link;
}
add_filter('post_type_link', 'filter_post_type_link', 10, 2);


// my own function to do what get_category_parents does for other taxonomies
function get_taxonomy_parents($id, $taxonomy, $link = false, $separator = '/', $nicename = false, $visited = array()) {    
$chain = '';   
$parent = &get_term($id, $taxonomy);

if (is_wp_error($parent)) {
    return $parent;
}

if ($nicename)    
    $name = $parent -> slug;        
else    
    $name = $parent -> name;

if ($parent -> parent && ($parent -> parent != $parent -> term_id) && !in_array($parent -> parent, $visited)) {    
    $visited[] = $parent -> parent;    
    $chain .= get_taxonomy_parents($parent -> parent, $taxonomy, $link, $separator, $nicename, $visited);
}
if ($link) {
    // nothing, can't get this working :(
} else    
    $chain .= $name . $separator;
return $chain;    
}


//Rewrite rules
add_filter('rewrite_rules_array', 'gallery_rewrite_rules');
function gallery_rewrite_rules($rules) {
$newRules  = array();

$newRules['gallery/(.+)/(.+)/(.+)/?$'] = 'index.php?gallery=$matches[3]';   
$newRules['gallery/(.+)/(.+)/?$'] = 'index.php?gallery_cat=$matches[2]'; 
$newRules['gallery/(.+)/?$']      = 'index.php?gallery_cat=$matches[1]';

return array_merge($newRules, $rules);
}

Now, I think the problem is where I need to add the following line to the rewrite rules array:

$newRules['gallery/(.+)/(.+)/?$'] = 'index.php?gallery=$matches[2]';

because my custom structure won't always have the post name as the 3rd uri segment because if the CPT is only under a parent taxonomy, than it will be in the 2nd uri segment.

But when I add that, it breaks things. My best guess is that it is clashing with this line...

$newRules['gallery/(.+)/(.+)/?$'] = 'index.php?gallery_cat=$matches[2]';

Jeff, who helped me make it this far from this thread, said the following...

If you'll have varying levels of nested terms then you'll need to write a function to check whether the the last uri segment is a custom post type or a taxonomy term to know which rule to add

Anyone know how this can be accomplished?

Thanks