2

Is it possible to create a permalink/template structure like this:

www.example.com/attractions                  <-- post type archive
www.example.com/attractions/museums          <-- taxonomy archive
www.example.com/attractions/van-gogh-museum  <-- custom post type

After trying for days I can only get 1 of the 2 combinations to work: either post type + post type archive or taxonomy + taxonomy archive

Not sure what to do next: am I overlooking something, do I need a plugin, should I set this up differently?

=== edit ===

(just thinking out loud)

At the moment I´m using post type and taxonomy rewrite capabilities, like this:

'rewrite' => array('slug'=>'attractions'),

but maybe I should go another route and manually set "attractions" as the parent page of both the custom post type and the taxonomy archive.

===/edit===

I hope some one can point me in the right direction. All suggestions are much appreciated!

1
  • I´ve asked this question on the WP Support forums as well, will update this thread when I receive an answer.
    – Mattvic
    Commented Oct 27, 2011 at 16:20

2 Answers 2

1
// Add the new post_type and taxonomy
add_action('init', function(){
    // Add a post_type named 'testype'
    register_post_type($postype = 'testype', $arguments = array(
        'name'                  => $postype,
        'label'                 => 'Test',
        'public'                => true,
        'exclude_from_search'   => true,
        'capability_type'       => 'post',
        'supports'              => array(
            'title', 'excerpt', 'editor', 'custom-fields', 'comments',
        ),
        'show_in_nav_menus'     => false,
        'hierarchical'          => false,
        'publicly_queryable'    => true,
        'rewrite'               => array(
            'with_front'    => false,
            'pages'         => true,
            'feeds'         => true,
            'slug'          => 'test/read',
            // This is my custom work, not stock functionality
            // 'permastruct'    => '%id%'
        ),
        'query_var'             => 'testing',
        'has_archive'           => true,
        'show_ui'               => true,
        'can_export'            => true,
        '_builtin'              => false,
    ));
    register_taxonomy($postax = 'testax', $postype, array(
        'label'         => 'Tax',
        'hierarchical'  => false,
        'rewrite'       => array(
            'with_front'        => false,
            'pages'             => true,
            'feeds'             => true,
            'slug'              => 'test/browse',
        ),
        'public'            => true,
        'show_ui'           => true,
        'query_var'         => true,
        'show_in_nav_menus' => true,
        'show_tagcloud'     => false,
    ));
});
// Add the 'snippet' postype to the loop.
add_action('pre_get_posts', function(\WP_Query $query){
    if(!is_archive()) return; // Only for archives!
    // If suppress_filters is on, bail here :) (no idea what this really does)
    if(!empty($query->query_vars['suppress_filters'])) return;
    // Add a new post type to the loop if we are visiting the archive
    // of a taxonomy assigned to the post.
    if(!empty($query->query_vars['testax'])){
        $post_types = $query->get('post_type');
        if(empty($post_types)) $post_types = array('post');
        elseif(is_string($post_types)) $post_types = array($post_types);
        // Add the new post_type now
        $query->set('post_type', array_merge($post_types, array('testype')));
    }
    return;
});

Cheers! This is fully functional and commented. It has PHP 5.3 syntax as I don't do 5.2 anymore and I'm also sort of lazy. I'm sure you can handle the function extraction if you still use 5.2.

Let me know how it goes, and if you have questions.

3
  • Wow thanks so much for your help :) I´m running PHP 5.3 - I´ll test it tonight!
    – Mattvic
    Commented Oct 27, 2011 at 18:52
  • @Mattvic Make sure you refresh the Permalinks by visiting the Settings - Permalinks page and saving settings. IMPORTANT! Commented Oct 27, 2011 at 19:52
  • During the week I´ve tried your code, but unfortunately I couldn´t it to work. Basically, I can´t have the post type and the taxonomy archive on the same level, like this: example.com/attractions/ (<- post archive) example.com/attractions/history-museum (<- custom post) example.com/attractions/museums/ (<- taxonomy archive) I think that my question was a bit confusing as well. I´ll post a new, more specific question. Thanks for your help anyway, much appreciated!
    – Mattvic
    Commented Nov 4, 2011 at 21:01
0

See if this answer helps: How to add a link(href) so when i click it, it renders (shows all) posts of custom post type (rich_media), from the current category?

You need to add the new post_type to the pre_get_posts to have stuff work properly in Term Archives (Categories, Tags). And make sure you enable archive on your custom post type.

1
  • Thanks, but I don´t think that´s the solution. I ´m probably trying to do something wrong, rewriting both the post type archive and the taxonomy archive to the same URL...
    – Mattvic
    Commented Oct 27, 2011 at 17:43

Your Answer

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

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