WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Custom query url for taxonomies on multiple post types (9 posts)

  1. Tammy Hart
    Member
    Posted 2 years ago #

    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.

  2. creding
    Member
    Posted 2 years ago #

    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;
    }

  3. Tammy Hart
    Member
    Posted 2 years ago #

    yay, that works beautifully. thanks :)

  4. Tammy Hart
    Member
    Posted 2 years ago #

    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

  5. creding
    Member
    Posted 2 years ago #


    <?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" ) ;

  6. Tammy Hart
    Member
    Posted 2 years ago #

    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

  7. creding
    Member
    Posted 2 years ago #

    Fixed my error in the previous :)

  8. parisvega
    Member
    Posted 2 years ago #

    awesome

  9. creding
    Member
    Posted 2 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic