【问题标题】:WordPress custom post type link are changed but returns 404WordPress 自定义帖子类型链接已更改但返回 404
【发布时间】:2020-11-26 11:01:35
【问题描述】:

WordPress 自定义帖子类型链接已更改但返回 404。

我已经创建了一个这样的自定义帖子类型,

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

网址更改为,

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

网址从 http://example.com/resource/my-first-note/http://example.com/notebook/note-category/my-first-note

但它返回404

【问题讨论】:

标签: wordpress custom-post-type


【解决方案1】:

您是否尝试过重新生成永久链接?

从后端:设置 -> 固定链接 -> 保存

【讨论】:

    猜你喜欢
    • 2012-01-01
    • 2014-05-20
    • 1970-01-01
    • 2022-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多