自定义帖子类型/分类URL重写

时间:2017-10-08 作者:lukgoh

我注册了一个自定义帖子类型:“jobs”

// set up labels
    $labels = array(
        \'name\'               => \'Jobs\',
        \'singular_name\'      => \'Job\',
        \'add_new\'            => \'Add New Job\',
        \'add_new_item\'       => \'Add New Job\',
        \'edit_item\'          => \'Edit Job\',
        \'new_item\'           => \'New Job\',
        \'all_items\'          => \'All Jobs\',
        \'view_item\'          => \'View Job\',
        \'search_items\'       => \'Search Jobs\',
        \'not_found\'          =>  \'No Jobs Found\',
        \'not_found_in_trash\' => \'No Jobs found in Trash\',
        \'menu_name\'          => \'Jobs\',
    );

    //register post type
    register_post_type( \'jobs\', array(
        \'labels\'              => $labels,
        \'has_archive\'         => \'jobs\',
        \'public\'              => true,
        \'supports\'            => array( 
            \'title\', 
            \'editor\', 
            \'excerpt\', 
            \'thumbnail\',
            \'page-attributes\' 
        ),
        \'exclude_from_search\' => false,
        \'capability_type\'     => \'post\',
        \'query_var\'           => true,
        \'menu_icon\'           => \'dashicons-book-alt\',
        \'rewrite\'             => array( 
            \'slug\'            => \'jobs/%job_status%\',
            \'with_front\'      => false
        ),

        )

    );
和自定义分类:“状态”

$labels = array(
        \'name\'              => _x( \'Status\', \'taxonomy general name\', \'trademanager\' ),
        \'singular_name\'     => _x( \'Status\', \'taxonomy singular name\', \'trademanager\' ),
        \'search_items\'      => __( \'Search Statuses\', \'trademanager\' ),
        \'all_items\'         => __( \'All Statuses\', \'trademanager\' ),
        \'parent_item\'       => __( \'Parent Status\', \'trademanager\' ),
        \'parent_item_colon\' => __( \'Parent Status:\', \'trademanager\' ),
        \'edit_item\'         => __( \'Edit Status\', \'trademanager\' ),
        \'update_item\'       => __( \'Update Status\', \'trademanager\' ),
        \'add_new_item\'      => __( \'Add New Status\', \'trademanager\' ),
        \'new_item_name\'     => __( \'New Status Name\', \'trademanager\' ),
        \'menu_name\'         => __( \'Status\', \'trademanager\' ),
    );

    $args = array(
        \'hierarchical\'      => true,
        \'labels\'            => $labels,
        \'show_ui\'           => true,
        \'show_admin_column\' => true,
        \'query_var\'         => true,
        \'rewrite\'           => array( \'slug\' => \'jobs/%job_status%\', \'with_front\' => false ),
    );
我希望URL是:jobs/**特定状态(如类别)**/post

我找到了这个函数:

function tm_jobs_permalinks( $post_link, $post ){
    if ( is_object( $post ) && $post->post_type == \'jobs\' ){
        $terms = wp_get_object_terms( $post->ID, \'status\' );
        if( $terms ){
            return str_replace( \'%job_status%\' , $terms[0]->slug , $post_link );
        }
    }
    return $post_link;
}

add_filter( \'post_type_link\', \'tm_jobs_permalinks\', 1, 2 );
这很好地创建了url,但它只返回404错误。我做错了什么?

2 个回复
SO网友:fatihsolhan

打开Settings > Permalinks 只需单击“保存设置”按钮,无需任何更改。

SO网友:lukgoh

已解决:

已将分类法重写为:

\'rewrite\' => array( \'slug\' => \'jobs\', \'with_front\' => false ),
将post类型重写更改为:

\'rewrite\' => true
添加了以下功能:

function tm_jobs_generating_rule($wp_rewrite) {
    $rules = array();
    $terms = get_terms( array(
        \'taxonomy\' => \'status\',
        \'hide_empty\' => false,
    ) );

    $post_type = \'jobs\';
    foreach ($terms as $term) {    

        $rules[\'jobs/\' . $term->slug . \'/([^/]*)$\'] = \'index.php?post_type=\' . $post_type. \'&jobs=$matches[1]&name=$matches[1]\';

    }
    // merge with global rules
    $wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_filter(\'generate_rewrite_rules\', \'tm_jobs_generating_rule\');

function tm_change_link( $permalink, $post ) {

    if( $post->post_type == \'jobs\' ) {
        $resource_terms = get_the_terms( $post, \'status\' );
        $term_slug = \'\';
        if( ! empty( $resource_terms ) ) {
            foreach ( $resource_terms as $term ) {
                // The featured resource will have another category which is the main one
                if( $term->slug == \'featured\' ) {
                    continue;
                }
                $term_slug = $term->slug;
                break;
            }
        }
        $permalink = get_home_url() ."/jobs/" . $term_slug . \'/\' . $post->post_name;
    }
    return $permalink;
}
add_filter(\'post_type_link\', \'tm_change_link\',10,2);
所有工作正常。

结束

相关推荐