简体   繁体   English

Wordpress 自定义永久链接被忽略

[英]Wordpress custom permalink is ignored

I purchased a theme, which has the custom post type talent and now i'm attempting to have a custom permalink for this post type.我购买了一个具有自定义帖子类型talent的主题,现在我正在尝试为这种帖子类型设置一个自定义永久链接。

I've added this to functions.php我已将此添加到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);
}   

I added %talent-cat% to the permalink settings custom structure and nothing is being changed.我将%talent-cat%添加到永久链接设置自定义结构中,但没有任何更改。

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

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

How do i solve?我该如何解决? I want my code to permalink function to overwrite/have priority any plugin.我希望我的代码永久链接功能覆盖/优先任何插件。

Usually you need to change permalinks structures using priority (as you done) and通常您需要使用优先级(如您所做的那样)和

register_post_type register_post_type

, which allows to take any new structure for any post_type. ,它允许为任何 post_type 采用任何新结构。 I suppose the problem here is that you did not take into account this, so take a look to a more clean approach .我想这里的问题是你没有考虑到这一点,所以看看更干净的方法

It is hard going deeper in question, I suppose, without any knowledge about your theme.我想,如果对您的主题一无所知,就很难深入探讨这个问题。 This because some premium themes does NOT allow to take into account these feature ("spaghetti code" and bad-formed structures / not conventional programming is very common there).这是因为一些高级主题不允许考虑这些功能(“意大利面条代码”和格式错误的结构/非传统编程在那里很常见)。

Please consider that you are working on latest URL version you working, in any case: ie you can also force rewrite rules with this function:在任何情况下,请考虑您正在使用最新的 URL 版本:即,您还可以使用此功能强制重写规则:

flush_rewrite_rules();

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

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