首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Wordpress自定义post类型永久链接重写post问题

Wordpress自定义post类型永久链接重写post问题
EN

Stack Overflow用户
提问于 2018-01-25 01:34:27
回答 1查看 893关注 0票数 1

我刚刚设法为我的自定义帖子类型重写了URL固定链接结构,这正是我想要的工作方式,然而,自从添加这些重写后,我的博客帖子开始转到404页面。

在我的functions.php文件中有:

代码语言:javascript
复制
'rewrite' => array( 'slug' => '%companies%/projects', 'with_front' => false),

这在我注册它的自定义帖子类型中。

在我的functions.php的底部,我有这个代码片段:

代码语言:javascript
复制
function wpa_show_permalinks( $post_link, $post ){
    if ( is_object( $post ) && $post->post_type == 'projects' ){
        $terms = wp_get_object_terms( $post->ID, 'companies' );
        if( $terms ){
            return str_replace( '%companies%' , $terms[0]->slug , $post_link );
        }
    }
    return $post_link;
}
add_filter( 'post_type_link', 'wpa_show_permalinks', 1, 2 );

我看不出为什么这会影响我的帖子?我已经通过管理员保存了固定链接。

EN

回答 1

Stack Overflow用户

发布于 2018-01-25 16:30:59

我之前在我的网站上做过类似的事情,所以我试着重写我的代码来遵循你的结构,但你应该看看变量,并用你的数据替换custompost和custompost-type。

最初发表在我的博客:https://windowspros.ru/url-rewrites-custom-post-type-permalinks-wordpress/

代码语言:javascript
复制
// REMOVE OLD PERMALINKS IF EXSIT

add_action( "after_setup_theme", 'custom_remove_permalink', 1);
function custom_remove_permalink(){
    remove_action( "after_setup_theme", 'active_permalinks');
}

// CREATE CUSTOM RULES

add_action( "after_setup_theme", 'custom_active_permalinks');
function custom_active_permalinks() {
    global $wp_rewrite;
    $wp_rewrite->add_rewrite_tag("%custompost%", '([^/]+)', "custompost=");
    $wp_rewrite->add_rewrite_tag( "%custompost-type%", '(.+?)', "custompost-type=" );
    $wp_rewrite->add_rewrite_tag( "%companies%", '(.+?)', "companies=" );
    $rewrite_args = array();
    $rewrite_args['walk_dirs'] = false;
    add_rewrite_rule( '([^/]+)/projects/?$', 'index.php?category_name=$matches[1]&post_type=custompost', 'top' );
    $wp_rewrite->add_permastruct('custompost', '%companies%/projects/%custompost%', $rewrite_args);

}

// Make sure that all links on the site, include the related texonomy terms

add_filter( 'post_type_link', 'custom_custompost_permalink', 10, 2 );
function custom_custompost_permalink( $permalink, $post ) {
    if ( $post->post_type !== 'custompost' ) {
        return $permalink;
    }

    $companies = get_post_meta($post->ID, 'company', true );

    if ( ! $companies ) {
        $permalink = str_replace( '%companies%', 'defaultvalue', $permalink );
    } else {
        $permalink = str_replace( '%companies%', $companies , $permalink );
    }

    $terms = wp_get_post_terms($post->ID, 'custompost-type', array( 'orderby' => 'parent', 'order' => 'DESC' ));

    if ( ! $terms ) {
        $permalink = str_replace( '%custompost-type%', '_yoast_wpseo_primary_category', $permalink );
    } else {
        $permalink = str_replace( '%custompost-type%', $terms[0]->slug , $permalink );
    }

    $authordata = get_userdata($post->post_author);
    $author = $authordata->user_nicename;
    if ( ! $author ) {
        $permalink = str_replace( '%author%', 'author', $permalink );
    } else {
        $permalink = str_replace( '%author%', $author , $permalink );
    }

    return $permalink;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48428487

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档