Monday, 27 May 2013

How to create a permalink for custom taxonomies and custom post types like base-name/parent-tax/child-tax/custom-post-type-name

How to create a permalink for custom taxonomies and custom post types like base-name/parent-tax/child-tax/custom-post-type-name

I found this link too much helpful. This is what I code so far and got it working for all scenarios except two scenarios.
register_post_type( 'article',
    array(
        'labels' => array(
            'name' => 'Article',
            'singular_name' => 'Article',
            'add_new' => 'Add Article',
            'add_new_item' => 'Add New Article',
            'edit' => 'Edit',
            'edit_item' => 'Edit Article',
            'new_item' => 'New Article',
            'view' => 'View',
            'view_item' => 'View Article',
            'search_items' => 'Search Article',
            'not_found' => 'No Article',
            'not_found_in_trash' => 'No Post found in Article',
            'parent' => 'Parent Article'
        ),

        'public' => true,
        'hierarchical' => true,
        'menu_position' => 15,
        'supports' => array( 'title', 'editor', 'comments', 'thumbnail' ),
        'taxonomies' => array( '' ),
        'menu_icon' => get_template_directory_uri().'/img/icon_article.png',
        'query_var' => true,
        'rewrite' => array(
            'slug' => 'articles/%article_category%',
            'with_front' => true
        ),
        'has_archive' => 'articles',
        'register_meta_box_cb' => 'add_article_metaboxes'
    )
);



register_taxonomy('article_category','article',array(
    'hierarchical' => true,
    'labels' => $labels,
    'query_var' => true,
    'rewrite' => array('slug' => 'articles', 'hierarchical' => true),
  ));

add_filter('rewrite_rules_array', 'mmp_rewrite_rules');
function mmp_rewrite_rules($rules) {
    $newRules  = array();
    $newRules['articles/(.+)/(.+)/(.+)/?$'] = 'index.php?article=$matches[3]';
    $newRules['articles/(.+)/(.+)/?$'] = 'index.php?article=$matches[2]';
    $newRules['articles/(.+)/?$'] = 'index.php?article_category=$matches[1]';

    return array_merge($newRules, $rules);
}

function filter_post_type_link($link, $post)
{
    if ($post->post_type != 'article')
        return $link;

    if ($cats = get_the_terms($post->ID, 'article_category'))
    {
        $link = str_replace('%article_category%', get_taxonomy_parents(array_pop($cats)->term_id, 'article_category', 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 wo

No comments:

Post a Comment