如何在 wordpress 中创建自定义分页固定链接 [英] How to create Custom Pagination Permalinks in wordpress

查看:36
本文介绍了如何在 wordpress 中创建自定义分页固定链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 wordpress 博客中有一篇包含多页的文章.例如,如果我的博客中有以下链接:

I have an article with several pages in my wordpress blog. if for example i have the following link in my blog :

http://example.com/heartbreaking-photos知道如何更改第二页的链接

http://example.com/heartbreaking-photos any idea how can i change the link of the second page from

http://example.com/heartbreaking-photos/2http://example.com/heartbreak-photos/CUSTOM-STRING

CUSTOM-STRING 旨在成为页面内的自定义标题

CUSTOM-STRING aimed to be a custom title inside the page

推荐答案

要做到这一点,您需要做两件事:

To achieve this, you will need to do 2 things:

  1. 禁用默认的 WordPress 规范重定向 - 这是必要的,因为 WordPress 在遇到页面参数时总是会重定向到 /2/ 页面URL 或查询参数.

  1. Disable the default WordPress canonical redirect - this is necessary, because WordPress will always redirect to the /2/ page when it encounters the page parameter in the URL or query args.

添加自定义重写规则以将自定义标题定向到页面的第二页 - 这对于允许您想要的链接格式来说是必不可少的.

Add a custom rewrite rule to direct your custom title to the second page of your page - this is essentially necessary to allow the link format that you want.

就代码而言,这就是您所需要的(这是一个可行的解决方案,我刚刚在本地进行了测试):

In terms of code, this is what you need (this is a working solution, I've just tested it locally):

// Removes the canonical redirection
remove_filter( 'template_redirect', 'redirect_canonical' );

// Add custom rewrite rules
add_action( 'init', 'my_add_custom_rewrite_rules' );
function my_add_custom_rewrite_rules() {
    // Slug of the target page
    $page_slug = 'heartbreaking-photos';

    // Page number to replace
    $page_num = 2;

    // Title you wish to replace the page number with
    $title = 'custom-string';

    // Add the custom rewrite rule
    add_rewrite_rule(
        '^' . $page_slug . '/' . $title . '/?$',
        'index.php?pagename=' . $page_slug . '&page=' . $page_num, 'top'
    );
}

您可能需要在此处配置或更改三项内容:

There are three things you might want to configure or change here:

  1. $page_slug - 这是您页面的 slug,在您的情况下,这是 heartbreaking-photos
  2. $page_num - 您的分页页数,在您的情况下为 2
  3. $title - 您希望使用的标题而不是页码 2.
  1. $page_slug - this is the slug of your page, in your case this is heartbreaking-photos
  2. $page_num - the number of your pagination page, in your case this is 2
  3. $title - the title you wish to use instead of your page number 2.

您可以随意更改代码,或复制它以涵盖更多其他情况,与此类似.

Feel free to alter the code as you wish, or copy it to cover more additional cases, similar to this one.

编辑

重要提示:使用代码后,请转到设置">永久链接",然后单击保存更改"按钮.这将重建您的重写规则,并且是解决方案工作所必需的.

Important: Once you use the code, go to Settings > Permalinks and click the "Save Changes" button. This will rebuild your rewrite rules, and is necessary for the solution to work.

希望有所帮助.如果您有任何问题,请告诉我.

Hope that helps. Let me know if you have any questions.

这篇关于如何在 wordpress 中创建自定义分页固定链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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