繁体   English   中英

Wordpress 自定义永久链接被忽略

[英]Wordpress custom permalink is ignored

我购买了一个具有自定义帖子类型talent的主题,现在我正在尝试为这种帖子类型设置一个自定义永久链接。

我已将此添加到functions.php

add_filter('post_link', 'talent_permalink', 99, 3);
add_filter('post_type_link', 'talent_permalink', 99, 3);

function talent_permalink($permalink, $post_id, $leavename) {
    if (strpos($permalink, '%talent-cat%') === FALSE) return $permalink;
        // Get post
        $post = get_post($post_id);
        if (!$post) return $permalink;

        // Get taxonomy terms
        $terms = wp_get_object_terms($post->ID, 'talent_category');   
        if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug;
        else $taxonomy_slug = 'all';

    return str_replace('%talent-cat%', $taxonomy_slug, $permalink);
}   

我将%talent-cat%添加到永久链接设置自定义结构中,但没有任何更改。

我想要什么http://example.com/work/%talent-cat%/model/%postname%/

目前发生了什么http://example.com/work/talent/%postname%/

我该如何解决? 我希望我的代码永久链接功能覆盖/优先任何插件。

通常您需要使用优先级(如您所做的那样)和

register_post_type

,它允许为任何 post_type 采用任何新结构。 我想这里的问题是你没有考虑到这一点,所以看看更干净的方法

我想,如果对您的主题一无所知,就很难深入探讨这个问题。 这是因为一些高级主题不允许考虑这些功能(“意大利面条代码”和格式错误的结构/非传统编程在那里很常见)。

在任何情况下,请考虑您正在使用最新的 URL 版本:即,您还可以使用此功能强制重写规则:

flush_rewrite_rules();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM