Wordpress Custom URL Rewrites

…衆ロ難τιáo~ 提交于 2019-12-03 21:49:18

Rewrite rules in WordPress don't quite work like how you're expecting them to. All rewrite rules map to a file handler (almost always index.php), not another URL.

Here is a shortened code example;

$rules['catalog/(.*)/?'] = 'index.php?pagename=catalog&tl=$matches[1]';
array_push($query_vars, 'tl'); // note query var should match your query string

This would map catalog/whatever to the WordPress page 'catalog', and pass along the query var 'tl'. You could create a page template for 'catalog', then pick up the value of 'tl' using get_query_var('tl').

Also avoid using query vars like id - use something more unique (like 'tl') to avoid clashes with WordPress.

And don't flush rules on every init! It's bad practice, and will write to .htaccess and call database updates on every page load!

WordPress will always flush permalinks whenever you update a post or your permalink structure (simply update your permalinks when you make changes to your code).

This article over at Raazorlight goes into a bit more detail on the process of grabbing querystrings and URL rewriting in WP.

Also, check the comments there to see why calling flushRules() may not be necessary if you re-save permalinks. Optionally, flushRules() can be called just once during plugin activation callback.

Digging deeper, commenter 'pmdci' links over to an instructive post/saga on the related topic of passing a query to a custom post type using a custom taxonomy.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!