更改 WordPress 上的帖子永久链接结构以使用自定义分类法

发布于 08-31 23:18 字数 439 浏览 17 评论 0原文

我想更改 WordPress 3.0-beta1 上的帖子永久链接架构以使用新的自定义分类法。

今天我可以使用 /%category%/%postname%//my-category/my-post/ URL,这很好,但我需要使用另一个分类法“类”一。

我尝试使用 /%acervo%/%postname%/ 但我的网址在 URL 上带有 %acervo% 而不是“Acevo”(我的分类名称)该帖子所属的。

我发现了与 WP_Rewrite 相关的内容,但没有成功......

I want to change the post permalink schema on my WordPress 3.0-beta1 to use my new custom taxonomy.

Today I can use /%category%/%postname%/ and the /my-category/my-post/ URL, that's nice but I need to use another taxonomy instead "category" one.

I tried to use /%acervo%/%postname%/ but my URLs came with %acervo% on the URL instead the name of the "Acevo" (my taxonomy name) wich the post belongs to.

I found something related to WP_Rewrite but without sucess...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

您可以尝试使用 WordPress 插件,无类别库,并且然后使用帖子名通配符对分类进行硬编码,如下所示:

/acervo/%postname%/

请注意,acervo 没有百分号,因为它是“硬编码”而不是通配符。

You could try using the WordPress plugin, No Category Base, and then hard code the taxonomy in with the postname wildcard, like so:

/acervo/%postname%/

Note that acervo does not have the percentage signs since it's "hard coded" and not a wildcard.

天气好吗我好吗2024-09-07 23:18:47

只需在Dashboard/Settings/Permalinks中更改您的类别库即可,无需删除类别库然后再次添加。

Simply change your category base in Dashboard/Settings/Permalinks No need to get rid of the category base and then add it in again.

披肩女神2024-09-07 23:18:47

我明白了...将永久链接结构更改为 /%acervos%/%postname%/,然后深入到 WP_Rewrite 并添加一个新的“替换标记”来替换 %acervos% > 与 (.*) 正则表达式。

I get it... Changed the permalink structure to /%acervos%/%postname%/ and then dived into WP_Rewrite and added a new "replacement tag" replacing %acervos% with (.*) regexp.

还在原地等你2024-09-07 23:18:47

这应该能解决问题。

function acervo_permalink($permalink, $post_id, $leavename){
    if (get_option('permalink_structure') != ''){
        $post = get_post($post_id);
        $rewritecode = array(
            '%acervo%'
        );
        if (strpos($permalink, '%acervo%') !== FALSE){   
            $terms = wp_get_object_terms($post->ID, 'acervo');  
            if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $acervo = $terms[0]->slug;
            else $acervo = '';
        }
        $rewritereplace = array(
            $acervo
        );
        $permalink = str_replace($rewritecode, $rewritereplace, $permalink);
    } 
    return $permalink;
}

This should to the trick.

function acervo_permalink($permalink, $post_id, $leavename){
    if (get_option('permalink_structure') != ''){
        $post = get_post($post_id);
        $rewritecode = array(
            '%acervo%'
        );
        if (strpos($permalink, '%acervo%') !== FALSE){   
            $terms = wp_get_object_terms($post->ID, 'acervo');  
            if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $acervo = $terms[0]->slug;
            else $acervo = '';
        }
        $rewritereplace = array(
            $acervo
        );
        $permalink = str_replace($rewritecode, $rewritereplace, $permalink);
    } 
    return $permalink;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文