0

I'm trying to adapt some code on calling a custom template via rewrite rule.

I have a custom taxonomy called "state". I want to create an archive for the taxonomy, that lists all the terms in that taxonomy.

http://www.site.com/state

Would load my custom template where I can query the terms and loop through them.

Alabama Alaska Arizone Arkinsas, etc

I know there is a possibility of some conflict if I had a page named "state" too, but that's a risk I can take.

The code in the tutorial works really well for

http://www.site.com/state/1 or http://www.site.com/state/2 but returns a 404 if there is no number captured.

and that number would be good for pagination if the terms list gets out of control.

Here's the code:

add_action('admin_init', 'handle_taxonomy_route');
function handle_taxonomy_route()
{
    add_rewrite_rule('^state/([^/]+)/?', 'index.php?state_var=$matches[1]', 'top');
}

add_filter('init', 'declare_vars');
function declare_vars()
{
    add_rewrite_tag('%state_var%', '([^&]+)');
}

add_filter('template_include', 'my_template', 1, 1);
function my_template($template)
{
    global $wp_query;

    if (isset($wp_query->query_vars['state_var'])) {
        return dirname(__FILE__) . '/state-list.php';
    }
    return $template;
}

From what I have tried so far, it doesn't appear to conflict with

http://www.site.com/state/alabama

Where Alabama is a term in the state taxonomy, but it should definitely only capture numbers.

A similar question went unanswered last year. How do I list terms of a custom taxonomy at i.e. domain.com/brands/

0

The 404 might be created because your regular expression requires the value after 'state/'. Please try to replace:

function handle_taxonomy_route()
{
    add_rewrite_rule('^state/([^/]+)/?', 'index.php?state_var=$matches[1]', 'top');
}

with:

function handle_taxonomy_route()
{
    add_rewrite_rule('^state/([^/]*)/?', 'index.php?state_var=$matches[1]', 'top');
}
  • This doesn't seem to make a difference. state_var is not added so the template_include function isn't finding it. I did a var_dump($wp_query) and it looks like I might be able to make it work with the pagename parameter. WordPress is looking at my URL like it thinks it is a page. – helgatheviking Mar 6 '13 at 0:45
0

I needed a better rewrite rule for proper pagination.

function kia_custom_rewrite() {

    // registers the state_var query variable
    add_rewrite_tag('%state_var%','([^&]+)');

    // handles site.com/state and pagination such as site.com/state/page/2
    add_rewrite_rule(
        'state(/page/([0-9]+)?)?/?$',
        'index.php?state_var=all&paged=$matches[2]',
        'top'
    );

}
// refresh/flush permalinks in the dashboard if this is changed in any way
add_action( 'init', 'kia_custom_rewrite', 99 );

The rewrite rule adds the query_var to check for here:

/*
 * Load custom templates
 * 1. Get a custom template for the /state "page"
 * 2. Get a custom template for the /state/some-state term
 */
function kia_template( $template ){
    global $wp_query;


    if ( 'all' == get_query_var('state_var') ) {
        $template = get_stylesheet_directory() . '/taxonomy-state-list.php';
    } 

    return $template;
}
add_filter( 'template_include', 'kia_template' );

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.