更改CPT固定链接以使用类别

时间:2019-06-11 作者:xaifu

我有一个称为教师的CPT和一个称为教师类型的自定义分类法。

我正在使用下面的代码重写CPT的url,而不是像:website。com/教师/帖子1

它将slug更改为使用类别:网站。com/教师类型/职位1

这是正确的,但现在我需要在CPT和slug中添加另一个分类法(年份),我需要这样:

网站com/教师类型/年份/职位1

我使用的代码与WPML兼容。

就是这样:

    function resources_cpt_generating_rule($wp_rewrite) {
    global $sitepress, $sitepress_settings;

    $has_filter = remove_filter( \'terms_clauses\', array( $sitepress, \'terms_clauses\' ) );
    $auto_adjust_ids = $sitepress_settings[\'auto_adjust_ids\'];
    $sitepress_settings[\'auto_adjust_ids\'] = 0;

    $rules = array();
    $terms = get_terms( array(
        \'taxonomy\' => \'teacher_types\',
        \'hide_empty\' => false,
    ) );

    $post_type = \'teachers\';

    foreach ($terms as $term) {    

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

    }

    // merge with global rules
    $wp_rewrite->rules = $rules + $wp_rewrite->rules;

    if ( $has_filter ) {
        add_filter( \'terms_clauses\', array( $sitepress, \'terms_clauses\' ) );
    }
    $sitepress_settings[\'auto_adjust_ids\'] = $auto_adjust_ids;

}
add_filter(\'generate_rewrite_rules\', \'resources_cpt_generating_rule\', 20);

function change_link( $permalink, $post ) {

    if( $post->post_type == \'teachers\' ) {
        $resource_terms = get_the_terms( $post, \'teacher_types\' );
        $term_slug = \'\';
        if( ! empty( $resource_terms ) ) {
            foreach ( $resource_terms as $term ) {
                // The featured resource will have another category which is the main one

                $term_slug = $term->slug;
                break;
            }
        }
        $permalink = apply_filters( \'wpml_permalink\', trailingslashit( get_home_url() ) . $term_slug . \'/\' . $post->post_name );
    }
    return $permalink;
}
add_filter(\'post_type_link\',"change_link",10,2);
有没有关于如何在url中连接这两个分类法的想法?

谢谢你的帮助!

1 个回复
SO网友:Chetan Vaghela

您需要使用register\\u post\\u type函数在下面的一行中更新您已经注册了自定义post类型的位置。

\'rewrite\' => array(\'slug\' => \'teachers/%cat%\')

要动态更改post类型的permalink,必须在函数中添加以下代码。php文件:

function change_link( $post_link, $id = 0 ) {
    $post = get_post( $id );
    if ( is_object( $post ) ) {
        $terms = wp_get_object_terms( $post->ID, array(\'teacher-type\',\'year\') );
        if ( $terms ) {
            return str_replace( \'%cat%\', $terms[0]->slug, $post_link );
        }
    }

    return  apply_filters(\'wpml_permalink\', $post_link );
}

add_filter( \'post_type_link\', \'change_link\', 1, 3 );

//load the template on the new generated URL otherwise you will get 404\'s the page

function generated_rewrite_rules() {

   add_rewrite_rule(
       \'^teachers/(.*)/(.*)/?$\',
       \'index.php?post_type=teachers&name=$matches[2]\',
       \'top\'
   );
}
add_action( \'init\', \'generated_rewrite_rules\' );
之后,需要刷新重写永久链接,转到wp-admin > Settings > permalinks. 只需使用“保存更改”按钮更新永久链接设置。

它将返回如下URL:

网站。com/teachers/teacher type/post1网站。com/teachers/year/post1谢谢!

相关推荐