简体   繁体   中英

Wordpress Rewrite Rule for Archive

So I'm having an issue with rewrite rules in WordPress.

When I input a custom query that isn't there, it reverts back to index.php, but I don't want it to, because I'm handling the query variables in the archive-(page).php.

<?php
    add_filter('query_vars', 'cat_query_vars');
    function cat_query_vars($qvars) {
        $qvars[] = 'a_type';
        return $qvars;
    }

    add_action('init', 'create_type_rewrite');
    function create_type_rewrite() {
        add_rewrite_rule('^atype/([^/]+)/?', 'index.php?post_type=cars&a_type=$matches[1]', 'top');
    }
?>

If I put in "Mercedes" and it's in the system, it will go to archive-cars.php, and use get_posts($args), where I have 'a_type' => $wp_query->query_vars['a_type'];. There is a global $wp_query.

I'm still having an issue though, say if I have a custom taxonomy "Mercedes" in there it will pull up results for all relating to "Mercedes" but if I put in "sadfasdf" it will default to index.php for some reason.

You add query vars like this...

function add_my_query_var($vars) {
    $vars[] = 'my_var';
    return $vars;
}

add_filter('query_vars', 'add_my_query_var');

the filter function is passed an array of the existing query vars, you need to append that array and then return it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM