WordPress:如何将生成的 URL(永久链接)转换为漂亮的 URL?

发布于 2024-08-26 03:24:11 字数 967 浏览 4 评论 0原文

在插件中,我生成一个指向类别的分页链接,
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

流绪微梦 2024-09-02 03:24:11

您看过 WP_Rewrite 吗?

Have you looked at the WP_Rewrite?

智商已欠费 2024-09-02 03:24:11

一体化 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.

失去的东西太少 2024-09-02 03:24:11

我的 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?

小嗲 2024-09-02 03:24:11

我猜你已经明白了,但我只是想把它告诉其他人。我想我理解你的处境。如果我正确阅读了您的问题,则更改管理永久链接设置不适用。

您可以获取类别 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?

伪心 2024-09-02 03:24:11

生成链接的正确方法是使用 get_category_linkadd_query_arg

在您的示例中,以下操作将起作用:

$category_link = add_query_arg( array('paged' => 5), get_category_link($category->term_id));

这将生成 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:

$category_link = add_query_arg( array('paged' => 5), get_category_link($category->term_id));

This will generate the URL and Wordpress will redirect to the correct page.

秋意浓 2024-09-02 03:24:11

查看此页面,它解释了相当好的永久链接。
http://codex.wordpress.org/Using_Permalinks

您应该在以下位置找到这些设置:

Wordpress->设置->固定链接

我认为的

固定链接

/%类别%/%帖子名称%

朝您想要的方向

您还可以尝试使用 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

Wordpress->Settings->Permalinks

I think

/%category%/%postname%

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文