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

WordPress custom post type link are changed but returns 404

programmeradmin0浏览0评论

WordPress custom post type link are changed but returns 404.

I have created a custom post type like this,

function create_notes_custom() {

    register_post_type( 'coaches_resources',
        array(
            'labels' => array(
                'name' => __( 'Coaches Resources' ),
                'singular_name' => __( 'Coaches Resource' ),
                'add_new' => __( 'Add New' ),
                'add_new_item' => __( 'Add New Resource' ),
                'edit' => __( 'Edit' ),
                'edit_item' => __( 'Edit Resource' ),
                'new_item' => __( 'New Resource' ),
                'view' => __( 'View' ),
                'view_item' => __( 'View Resource' ),
                'search_items' => __( 'Search Resources' ),
                'not_found' => __( 'No resources found' ),
                'not_found_in_trash' => __( 'No resource found in Trash' ),
                'parent' => __( 'Parent Resource' )
            ),
            'public' => true,
            'menu_position' => 5,
            'supports' => array( 'title' ),
            'taxonomies' => array( 'resource_chapter' ),
            'rewrite' => array( 'slug' => 'resource', 'with_front' => true ),
            'has_archive' => false,
            'hierarchical' => false
        )
    );
}
add_action('init', 'create_notes_custom');

URL is changed by,

function change_link( $permalink, $post ) {
    
    if( $post->post_type == 'coaches_resources' ) {
        $resource_terms = get_the_terms( $post, 'resource_chapter' );
        $term_slug = '';
        if( ! empty( $resource_terms ) ) {
            foreach ( $resource_terms as $term ) {
            
            
                if (! empty($term->slug))  {
                    
                    // 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() ."/notebook/" . $term_slug . '/' . $post->post_name;
    }
    return $permalink;

}
add_filter('post_type_link', "change_link", 10, 2);

URL is changed from / to

But it returns 404.

发布评论

评论列表(0)

  1. 暂无评论