WordPress.org

Support

Support » Plugins and Hacks » [Resolved] Custom query url for taxonomies on multiple post types

[Resolved] Custom query url for taxonomies on multiple post types

  • I’ve created a custom taxonomy called “Towns” and several Custom Post Types like “Announcements”, “Events, and “Sponsors”. I’m working on customizing the taxonomy archive page, taxonomy-town.php, to output the various content types like I want. But here’s my next challenge:

    I need to be able to link to an archive page of posts that are in a certain term and a certain post type. So for instance, I need all “Announcements” that are in the town of “Burley”.

    I imagine the URL looking something like Example.com/town/burley/announcements

    Any good tutorials or direction on this would be helpful.

Viewing 8 replies - 1 through 8 (of 8 total)
  • add_action('admin_init', 'flush_rewrite_rules');
    add_action('generate_rewrite_rules', 'town_rewrite_rules');

    function town_rewrite_rules( $wp_rewrite )
    {
    $new_rules = array(
    'town/(.+)/(.+)' => 'index.php?post_type=' . $wp_rewrite->preg_index(2) . '&town=' .$wp_rewrite->preg_index(1));

    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
    }

    yay, that works beautifully. thanks 🙂

    Now my only remaining issue is how to get it to have a custom layout for /town/burley, and a normal archive list for /town/burley/announcement


    <?php

    function get_custom_post_type_template($template) {
    global $wp_query;
    $post_type = $wp_query->query_vars['post_type'];
    if ( !isset($wp_query->query_vars['town'] ) )
    return $template;
    $tax = $wp_query->query_vars['town'];
    if ($post_type == 'announcements' && $tax ) {
    $template = dirname( __FILE__ ) . '/announcements-template.php';
    }
    return $template;
    }
    add_filter( "taxonomy_template", "get_custom_post_type_template" ) ;

    that works perfectly. And if I decide to only make one template for any post type, i just change that last if statement to if ($post_type && $tax )

    Chris, you’re the bomb dot com

    Fixed my error in the previous 🙂

    parisvega
    Member

    @parisvega

    awesome

    You could also change this
    $template = dirname( __FILE__ ) . ‘/announcements-template.php’;
    to this
    $template = dirname( __FILE__ ) . ‘/custom-‘.$post_type.’.php’;

    that would allow you to create custom templates that can be called by the current type with out rewriting a lot of code. I would also probably change

    $post_type == ‘announcements’
    to
    in_array( $post_type, array(‘announcements’, ‘other-posttype’, ‘other-posttype2’) )

    that will enable you to set some post types you want to use the custom template

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘[Resolved] Custom query url for taxonomies on multiple post types’ is closed to new replies.