ds3464 2013-02-08 18:49
浏览 60

Wordpress重写存档规则

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.

  • 写回答

1条回答 默认 最新

  • donte1234567 2013-02-08 18:52
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算