【问题标题】:Wordpress custom permalink is ignoredWordPress 自定义永久链接被忽略
【发布时间】:2019-05-06 18:36:53
【问题描述】:

我购买了一个主题,它具有自定义帖子类型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%/

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

【问题讨论】:

    标签: wordpress


    【解决方案1】:

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

    更改永久链接结构

    register_post_type

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

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

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

    flush_rewrite_rules();
    

    【讨论】:

      猜你喜欢
      • 2012-03-31
      • 2018-12-11
      • 2012-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-07
      • 1970-01-01
      • 2012-05-18
      相关资源
      最近更新 更多