简体   繁体   English

WordPress的永久链接生成404页

[英]Wordpress Permalink Generating 404 Page

I'm working on changing the permalink of a Custom Post Type to include the taxonomy prior to the post id. 我正在更改自定义帖子类型的固定链接,以在帖子ID之前包含分类法。 I'm capable of it displaying the taxonomy in the URL however when I go to the page I get a 404 Error. 我能够在URL中显示分类法,但是当我转到页面时,出现404错误。 It looks like the structure of the permalink is correct however the location of the post isn't synced up w/ the database location. 看起来永久链接的结构是正确的,但是帖子的位置没有与数据库位置同步。

Any help is appreciated and thanks in advance! 任何帮助表示赞赏,并在此先感谢!

Couple of notes: 几个注意事项:

  • My .htaccess file has mod rewrite on. 我的.htaccess文件已启用mod重写。
  • I've added %tax% to the permalink rewrite for the CPT 我已为CPT的永久链接重写添加了%tax%
  • I have archive turned on for the 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 );

Figured it out. 弄清楚了。 Needed to wp_rewrite: 需要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);
    }
}

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

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