【问题标题】:Page as archive with custom permalink structure not working页面作为具有自定义永久链接结构的存档不起作用
【发布时间】:2019-01-10 06:08:47
【问题描述】:

我有一个名为“references”的自定义帖子类型,并添加了一个函数,因此我可以将该类别用作永久链接的一部分。我像这样声明自定义 slug:

'slug'  => '/references/%category%',

然后使用过滤器对其进行操作:

function change_post_link($post_link, $id = 0) {
    $post = get_post($id);
    if ($post->post_type == 'references') {
        $terms = wp_get_object_terms($post->ID, 'category');
        if ($terms) {
            return str_replace('%category%', $terms[0]->slug, $post_link);
        }
    }
    return $post_link;
}

这部分工作正常,我得到的 URL 像“/references/cars/the-red-one”。

但是现在我不能在“/references/cars”中拥有页面(默认类型),它只有在我没有为自定义帖子类型使用占位符时才有效。

您有什么想法可以解决这个问题吗? 'has_archive' 是假的。

谢谢!

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    你可以这样使用

    查找分类标签:

    $categories = get_categories(); 
        foreach ($categories as $cat) 
        {
            $cat_slug = $cat->slug;
        }
    

    创建新网址:

    $new_url = esc_url(add_query_arg(array( 'category' => $cat_slug,), $old_url));
    

    【讨论】:

      【解决方案2】:

      这可能不是最好的解决方案,但至少现在可以了。给你:

      我添加了一个隐藏的虚拟帖子类型,其中包含 “references/dummy”。这似乎迫使 Wordpress 识别正确的(有争议的......)永久链接结构。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-18
        • 2017-05-06
        • 2013-04-10
        • 2019-02-25
        • 1970-01-01
        • 2014-05-14
        相关资源
        最近更新 更多