最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to replace custom post type slug with custom field value by maintaining a specific URL structure?

programmeradmin3浏览0评论

I want to replace a custom post type slug with custom field value.

I've used the below code for that:

add_rewrite_tag( '%employeetype%', 'our-team/([^/]+)' );
add_filter( 'post_type_link', function( $url, $post ){
    if ( 'developer' === $post->post_type ) {
        $customSlug = get_post_meta( $post->ID, 'employee_type', TRUE ) ? get_post_meta( $post->ID, 'employee_type', TRUE ) : 'developers'; // can be two vales develeopers and staff
        $url = str_replace( '%employeetype%', 'our-team/'.$customSlug, $url );
    }
    return $url;
}, 10, 2 );

I also want to maintain the below pages:
mysite/our-team/
mysite/our-team/developers - this page redirects to index page
mysite/our-team/staff - this page redirects to index page
mysite/our-team/developers/abcd (achieved with above code) works fine

Can we do anything here to achieve this? Thanks.

I want to replace a custom post type slug with custom field value.

I've used the below code for that:

add_rewrite_tag( '%employeetype%', 'our-team/([^/]+)' );
add_filter( 'post_type_link', function( $url, $post ){
    if ( 'developer' === $post->post_type ) {
        $customSlug = get_post_meta( $post->ID, 'employee_type', TRUE ) ? get_post_meta( $post->ID, 'employee_type', TRUE ) : 'developers'; // can be two vales develeopers and staff
        $url = str_replace( '%employeetype%', 'our-team/'.$customSlug, $url );
    }
    return $url;
}, 10, 2 );

I also want to maintain the below pages:
mysite/our-team/
mysite/our-team/developers - this page redirects to index page
mysite/our-team/staff - this page redirects to index page
mysite/our-team/developers/abcd (achieved with above code) works fine

Can we do anything here to achieve this? Thanks.

Share Improve this question edited Mar 30, 2020 at 17:54 chithra asked Mar 30, 2020 at 12:52 chithrachithra 1336 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 0

I've fixed this issue by adding the below code:

add_rewrite_rule('our-team/developers/?$','index.php?pagename=our-team/developers', 'top');
add_rewrite_rule('our-team/support-staff/?$','index.php?pagename=our-team/support-staff', 'top');
发布评论

评论列表(0)

  1. 暂无评论