簡體   English   中英

WordPress自定義帖子類型永久鏈接重寫帖子問題

[英]Wordpress custom post type permalink rewrite posts issue

我剛剛設法為我的自定義帖子類型重寫了URL永久鏈接結構,該結構正好按照我想要的方式工作,但是由於添加了這些重寫,我的博客帖子已開始進入404頁。

在我的functions.php文件中,我有:

'rewrite' => array( 'slug' => '%companies%/projects', 'with_front' => false),

這是在我注冊的“自定義帖子類型”中。

在我的functions.php的底部,我有以下代碼段:

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

我看不到這會影響我的帖子的原因嗎? 我已經通過管理員保存了永久鏈接。

我之前在我的網站上做過類似的事情,所以我嘗試重寫代碼以遵循您的結構,但是您應該查看變量,並用您的數據替換custompost和custompost-types。

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

// 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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM