0

I'm helping a small blog out that posts a few different "types" of articles. These article types are shared across categories. We want to put the article type in the URL. So the structure would look like this:

www.example.com/category/sub-category/article-type/article-name

I'm unsure of the best way to attribute these article types. Should they be sub-categories (so each category would have a subcategory of "how-to", "interview", "News", etc...

Should I use tags? Can I specify a specific tag to be in a URL (rather than all tags?)

Should I use a custom attribute?

Any guidance is good guidance, i'm at starting point zero...

  • 2
    Custom Post Type? – s_ha_dum May 22 '14 at 15:11
3

here's a way to add article type with tags.

First we add the taxonomy to our post

add_action('init', 'article_type_init');

function article_type_init() {
    if (!is_taxonomy('article_type')) {
        register_taxonomy( 'article_type', 'post', 
                   array(   
                        'hierarchical' => FALSE, 
                        'label' => __('Article Type'),  
                        'public' => TRUE, 
                        'show_ui' => TRUE,
                        'query_var' => 'article-type',
                        'rewrite' => true ) );
    }
}

Than we hook into the permalink generation, to be sure that we not receive 404 when we will try our new permalink structure

add_filter('post_link', 'article_type_permalink', 10, 3);
add_filter('post_type_link', 'article_type_permalink', 10, 3);
function rating_article_type($permalink, $post_id, $leavename) {
    if (strpos($permalink, '%article-type%') === FALSE) return $permalink;

        // Get post
        $post = get_post($post_id);
        if (!$post) return $permalink;

        // Get taxonomy terms
        $terms = wp_get_object_terms($post->ID, 'article_type');   
        if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug;
        else $taxonomy_slug = 'not-rated'; // add a default tag...

    return str_replace('%article-type%', $taxonomy_slug, $permalink);
}   

Finaly go to your permalink structure (General|Permalink) and change it to :

/%article-type%/%postname% 

And there you go, you're now able to see tags as permalink !

  • I think you are confusing %rating% and %article-type%. Also, the indentation is strange after the first if statement. I agree, using a custom taxonomy is probably the best way to go though. – Adam H May 22 '14 at 16:05
  • @Tspesh does that work ? if yes please approve it, thank you – Jonathan Lafleur Jun 4 '14 at 15:43

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.