Mixing custom post type and taxonomy rewrite structures?

Basically I want to achieve a glossary using custom post types and have some issues setting up rewrites the way I want them to be. I want it like that:

The main glossary URL:
http://example.com/glossary/

Glossary terms starting with letter A:
http://example.com/glossary/a/

The URL for a single glossary term:
http://example.com/glossary/a/atomic/

I actually achieved this using the code below but I’m sure that it’s a very awkward way to do it and I know it’s malfunctioning somewhere as the wrong templates are called when viewing the pages. Except for http://example.com/glossary/, where archive-sumo-glossary-term.php gets called as expected, the other two just activate index.php in my theme.

Here goes (functions.php in the theme):

add_action('init', 'create_glossary');
function create_glossary()
{
    register_post_type
    (
        'sumo-glossary-term',
        array
        (
            'labels' => array
            (
                'name' => _x('Glossary Terms', 'post type general name'),
                'singular_name' => _x('Glossary Term', 'post type singular name')
                # And so on …
            ),
            'supports' => array('title', 'editor', 'thumbnail'),
            'public' => true,
            'rewrite' => array
            (
                'slug' => 'glossary',
                'with_front' => false

            ),
            'query_var' => 'glossary-term',
            'has_archive' => true
        )
    );

    register_taxonomy
    (
        'sumo-glossary-letter',
        'sumo-glossary-term',
        array
        (
            'hierarchical' => true,
            'labels' => array
            (
                'name' => _x('Letters', 'taxonomy general name'),
                'singular_name' => _x('Letter', 'taxonomy singular name')
                # And so one
            ),
            'show_ui' => true,
            'query_var' => 'glossary-letter',
            'rewrite' => false
        )
    );
}

add_filter('post_type_link', 'glossary_term_permalink', 10, 4);
function glossary_term_permalink($post_link, $post, $leavename, $sample)
{
    if ($post->post_type == 'sumo-glossary-term')
    {
        $permalink = str_replace('glossary/', 'glossary/' . $post->post_name[0] . '/', $post_link);
    }
    return $permalink;
}

add_rewrite_rule('^glossary/([^/]*)?$','index.php?glossary-letter=$matches[1]','top');
add_rewrite_rule('^glossary/([^/]*)/([^/]*)?$','index.php?glossary-term=$matches[2]','top');

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can always override the template that will be called with the template_include or a related filter, but this might hide deeper problems with custom archives.

As I understand it, you want to use the following structure:

  • /glossary/ should be an archive page for all sumo-glossary-term posts
  • /glossary/[letter]/ should be an archive page for posts with the taxonomy term [letter] in the sumo-glossary-letter taxonomy
  • /glossary/[letter]/[term-name]/ should be an individual sumo-glossary-term post

This means that the first will load the template archive-sumo-glossary-term.php, the second will load taxonomy-sumo-glossary-letter.php, and the third will load single-sumo-glossary-term.php.

I achieved this in WordPress 3.2 by explicitly setting the taxonomy rewrite slug, and both the rewrite slug and the archive slug for the post type, and no other rewrite rules. Also, I registered the taxonomy first and the post type after it, to make sure the priorities were right (otherwise a URL like /glossary/f/page/2 goes to glossary term page instead of page 2 of glossary letter f).

add_action('init', 'create_glossary');
function create_glossary()
{

    register_taxonomy
    (
        'sumo-glossary-letter',
        array(),
        array
        (
            'hierarchical' => true,
            'labels' => array
            (
                'name' => _x('Letters', 'taxonomy general name'),
                'singular_name' => _x('Letter', 'taxonomy singular name')
                # And so one
            ),
            'show_ui' => true,
            'query_var' => 'glossary-letter',
            'rewrite' => array(
                'slug' => 'glossary',
            ),
        )
    );

    register_post_type
    (
        'sumo-glossary-term',
        array
        (
            'labels' => array
            (
                'name' => _x('Glossary Terms', 'post type general name'),
                'singular_name' => _x('Glossary Term', 'post type singular name')
                # And so on …
            ),
            'supports' => array('title', 'editor', 'thumbnail'),
            'public' => true,
            'rewrite' => array
            (
                'slug' => 'glossary/%sumo-glossary-letter%',
                'with_front' => false

            ),
            'query_var' => 'glossary-term',
            'has_archive' => 'glossary',
            'taxonomies' => array( 'sumo-glossary-letter' ),
        )
    );
}

add_filter('post_type_link', 'glossary_term_permalink', 10, 4);
function glossary_term_permalink($post_link, $post, $leavename, $sample)
{
    if ( false !== strpos( $post_link, '%sumo-glossary-letter%' ) ) {
        $glossary_letter = get_the_terms( $post->ID, 'sumo-glossary-letter' );
        $post_link = str_replace( '%sumo-glossary-letter%', array_pop( $glossary_letter )->slug, $post_link );
    }
    return $post_link;
}

Method 2

I haven’t personally used this, but what you may want to look at is “add_rewrite_rule”

http://codex.wordpress.org/Rewrite_API/add_rewrite_rule

also don’t forget to save out permalinks once you’ve changed them.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x