繁体   English   中英

WordPress的永久链接生成404页

[英]Wordpress Permalink Generating 404 Page

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

任何帮助表示赞赏,并在此先感谢!

几个注意事项:

  • 我的.htaccess文件已启用mod重写。
  • 我已为CPT的永久链接重写添加了%tax%
  • 我已为CPT打开存档

    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 );

弄清楚了。 需要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