WordPress:如何将生成的 URL(永久链接)转换为漂亮的 URL?
在插件中,我生成一个指向类别的分页链接,
即 http://localhost/?cat=17&paged=5< /代码>。该类别通过 slug 和 id 得知。
有没有办法将此 URL 转换为用户定义的“nice permalink”格式,
即 http://localhost/category/foo/page/5
?
(假设我们运行的是 Wordpress 2.8/2.9+)
编辑:由于对我想要实现的目标存在一些误解,所以这就是我所拥有的:
- 类别 ID 页码
- (即第五个该类别的存档页面)
使用这些数据,我可以创建该页面的 URL,例如 http://localhost/?cat=17&paged=5
,并且可以正常工作出色地。问题在于视觉本质:如果用户启用了相当多的永久链接,那么这将不太适合。 问题是:如何获得正确、漂亮的永久链接(即 http://localhost/category /foo/page/5
)来自(或针对)我所拥有的信息?
编辑2:显然,如果用户禁用了漂亮的永久链接,那么就没有漂亮的永久链接。我知道这一点,这确实不是问题的主题......目前,我们假设用户有一个任意永久链接设置。我生成一个链接,它应该看起来像用户想要的那样。
In a plugin, I am generating a paginated link to a category,
i.e. http://localhost/?cat=17&paged=5
. The category is known by slug and id.
Is there a way to have this URL converted to the user defined "nice permalink" format,
i.e. http://localhost/category/foo/page/5
?
(Assuming we're running Wordpress 2.8/2.9+)
Edit: Since there are some misunderstandings about what I'm trying to achieve, here's what I have:
- The category ID
- The page number (i.e. the fifth page of that category's archive)
With these data I can create the URL to that page, which would be, for example, http://localhost/?cat=17&paged=5
, and that works well. The problem is of a visual nature: If the user has pretty permalinks enabled, then this won't fit in very well.
Question is: How do I get the correct, pretty permalink (i.e. http://localhost/category/foo/page/5
) from (or for) the information I have?
Edit 2: Obviously if a user has pretty permalinks disabled, then there are no pretty permalinks. I know that, and that is really not subject of the question ... For the moment, let's just assume the user has an arbitrary permalink setting. I generate a link, and it should look like the user wants it to.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您看过 WP_Rewrite 吗?
Have you looked at the WP_Rewrite?
一体化 SEO 是否满足您的要求?实际上,我花了 30 美元购买了“专业”版本,非常值得。
Does All In One SEO meet your requirements? I actually bought the "pro" version for like $30 USD and it was well worth it.
我的 WordPress 安装 (2.9.2) 自动执行此操作 - 规范 URL 已内置到 Wordpress 中一段时间了。也许您的插件是 禁用此功能?
My Wordpress installation (2.9.2) does this automatically - canonical URLs have been built into Wordpress for some time. Maybe your plugin is disabling this feature?
我猜你已经明白了,但我只是想把它告诉其他人。我想我理解你的处境。如果我正确阅读了您的问题,则更改管理永久链接设置不适用。
您可以获取类别 ID,然后使用 get_category_link() 函数获取“漂亮”永久链接。然后将分页附加到末尾。
您可以分解永久链接以获取类别 ID 和分页号,然后使用上述步骤将其组合在一起。
这是您要找的吗?
I'm guessing you already figured it out but I just wanted to put this out for others. I think I understand your situation. If I've read your question correctly, changing admin permalink settings does not apply.
You can get the category ID then use
get_category_link()
function to get the 'pretty' permalink. Then append the pagination to the end.You could explode the permalink to get the category ID and pagination number, then use the step above to put it together.
Is this what you were looking for?
生成链接的正确方法是使用 get_category_link 和 add_query_arg
在您的示例中,以下操作将起作用:
这将生成 URL,Wordpress 将重定向到正确的页面。
The correct way to generate the link would be to use get_category_link and add_query_arg
In your example, the following would work:
This will generate the URL and Wordpress will redirect to the correct page.
查看此页面,它解释了相当好的永久链接。
http://codex.wordpress.org/Using_Permalinks
您应该在以下位置找到这些设置:
我认为的
朝您想要的方向
您还可以尝试使用 htaccess 重定向您的网址,以下是一些示例:
[http://]perishablepress.com/press/2008/02/06/permalink-evolution-定制和优化您的日期的wordpress-permalinks/
抱歉,darf bis jetzt nur ein link posten pfft...:-(
亲切的问候,
马哈特马尼奇
check this page out, it explains pretty permalinks pretty good.
http://codex.wordpress.org/Using_Permalinks
You should find these settings in
I think
in the direction what you want
You can also try to redirect your url using htaccess, here are some examples:
[http://]perishablepress.com/press/2008/02/06/permalink-evolution-customize-and-optimize-your-dated-wordpress-permalinks/
Sorry darf bis jetzt nur ein link posten pfft... :-(
kind regards,
Mahatmanich