【问题标题】:Custom Permalinks For CPT and pages with parent. Advanced WordpressCPT 和父页面的自定义永久链接。高级WordPress
【发布时间】:2021-09-26 09:46:02
【问题描述】:

所以,我创建了一个 CPT - Community Post: https://pastebin.com/9XSrvZAr

对于这个 CPT,我创建了两个自定义分类法:https://pastebin.com/fNhfpiM6

  1. community_post_domain
  2. community_post_type

我的目标是所有单个社区帖子的永久链接如下:
https://example.com/community/%POST_DOMAIN_SLUG%/%POST_SLUG%
因此,例如,如果我有一个标题为“Go Micro Services”的社区帖子,并将 community_post_domain 设置为名为 Kubernetes 的术语,则永久链接将为:
https://example.com/community/kubernetes/go-micro-services

这就是我进行永久链接操作的方式: https://pastebin.com/aP5rEBjY

另外,我创建了一个静态页面:Community,其中包含永久链接:https://example.com/community
到现在为止还挺好。当我尝试发布一个页面并且我希望它具有这样的永久链接时,就会出现问题:https://example.com/community/%PAGE_SLUG%
我创建页面,例如,Hello World。我将父级设置为Community 页面。当我单击Publish 时,页面已创建,在 WordPress 仪表板中,我看到该页面的永久链接符合预期:https://example.com/community/hello-world
但是,这是实际问题 - 当我尝试访问页面的永久链接时,我得到 404。
我尝试在 WP 仪表板中转到 Permalinks 并更新永久链接,但没有帮助。 这里有什么想法吗?

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    假设通常我们不会有太多的页面(5-10),解决这个问题的最简单最快的方法是不重写所有内容,为每个页面编写单独的rewrite_rules。

    add_rewrite_rule(
        '^community/hello-world/?',
        'index.php?page_id=123',
        'top'
    ); 
    

    你的问题的基础是这一行

    'rewrite' => array( 'slug' => 'community', 'with_front' => false ),
    

    当您注册自定义分类时,您使用

    'slug' => 'community'
    

    现在wordpress认为在这个链接https://example.com/community/hello-world/

    hello-world = 分类蛞蝓

    为了更准确地诊断此类问题并查看当前页面使用的重写规则 - 试试这个插件https://wordpress.org/plugins/query-monitor/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-10
      • 2012-05-18
      • 1970-01-01
      • 2016-06-08
      • 1970-01-01
      • 2013-03-05
      • 2018-12-19
      • 2012-09-12
      相关资源
      最近更新 更多