首页 文章

Wordpress永久链接生成404页面

提问于
浏览
0

我正在努力更改自定义帖子类型的永久链接,以在帖子ID之前包含分类 . 我能够在URL中显示分类法,但是当我转到页面时,我得到404错误 . 看起来永久链接的结构是正确的,但是帖子的位置没有与数据库位置同步 .

Any help is appreciated and thanks in advance!

Couple of notes:

  • 我的.htaccess文件有mod重写 .

  • 我已将%tax%添加到CPT的固定链接重写中

  • 我已为CPT打开档案

Code

function change_permalink( $link, $post ) {
    if ( ‘custom-post-type-name’ == get_post_type( $post ) ) {
        // Get post
        $post = get_post($post_id);
        if (!$post) return $permalink;
        // Get taxonomy terms
        $terms = wp_get_object_terms($post->ID, ’taxonomy-name’);
        if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug;

        else $taxonomy_slug = 'no-taxonomy-listed’;

        return str_replace('%tax%', $taxonomy_slug, $link);
    }
    return $link;
    }
    add_filter( 'post_type_link', ‘change_permalink’, 10, 2 );

1 回答

  • 0

    弄清楚了 . 需要wp_rewrite:

    add_action( 'wp_loaded', 'urban_permastructure' );
    function urban_permastructure($post) {
        if ( 'custom-post-type-name' == get_post_type( $post ) ) {
        global $wp_rewrite;
        $structure = '/cat/%tax%';
        $wp_rewrite->add_rewrite_tag("%tax%", '([^/]+)', "tax-type=");
        $wp_rewrite->add_permastruct('tax-type', $structure, false);
        }
    }
    

相关问题