WordPress的URL重写-很好的URL,名称来自SugarCRM [英] Wordpress url rewriting - nice URL with name from SugarCRM

查看:94
本文介绍了WordPress的URL重写-很好的URL,名称来自SugarCRM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Wordpress网站,该网站从SugarCRM加载了大部分内容。

I'm developing a Wordpress site, that loads most of the content from SugarCRM.

当前,我在URL上获得了Boats项目,例如

Currently I get the boats item on URL like

http://domain.com/boats/?id=123456789

,但我想使用从SugarCRM加载的名称作为好网址,例如

but I would like to have nice URL's, with the name that is loaded from SugarCRM, like

http://domain.com/boats/this-is-boat-name

是否可以在不创建自定义帖子类型和Wordpress中每个项目的帖子的情况下执行此操作?

Is there a way to do this without creating custom post type and a post for each item in Wordpress?

推荐答案

您可以尝试使用 add_rewrite_rule

让用户通过url domain.com/boats访问页面/ 12345 / this-is-boat-name /

然后,您可以在PHP中获取ID,就好像它是 $ _ GET 查询字符串。

Then you can pick up the ID in PHP as if it were a $_GET query string.

首先是重写规则;

$rule = '^(boats)/([^/]*)/([^/]*)/?';
$write = 'index.php?name=boats&id=$matches[1]&pagename=$matches[1]';
add_rewrite_rule($rule, $write, 'top');

然后使用过滤器,以便您可以像查询字符串一样访问页面URL

And then the filter so you can access the page URL as like a query string

add_filter('query_vars', 'foo_my_query_vars');
function foo_my_query_vars($vars){
    $vars[] = 'id';
    $vars[] = 'pagename';
    return $vars;
}

然后,在主题的页面模板上,可以选择(不可见)查询字符串值,例如:

Then, on the page template in your theme, you can pick up the (invisible) query string values like:

$id = get_query_var('id');
$pagename = get_query_var('pagename');
echo $id.' '.$pagename;

确保在添加重写规则并过滤到您的function.php中后刷新永久链接。

Make sure to flush the permalinks after adding the rewrite rules and filter into your functions.php

这篇关于WordPress的URL重写-很好的URL,名称来自SugarCRM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆