WordPress自定义帖子类型存档<帖子类型>.php不起作用


WordPress custom post type archive-<post type>.php not working

WordPress的新手,我一直在尝试为自定义帖子类型创建一个存档页面,当我单击链接localhost/websitename/wordpress/archive-event.php时,我得到了404。这是我注册帖子类型的代码:

add_action('init', 'event_register');
function event_register() {
    $labels = array(
        'name' => _x('Events', 'post type general name'),
        'singular_name' => _x('Event', 'post type singular name'),
        'add_new' => _x('Add New', 'event'),
        'add_new_item' => __('Add New Event'),
        'edit_item' => __('Edit Event'),
        'new_item' => __('New Event'),
        'view_item' => __('View Event'),
        'search_items' => __('Search Events'),
        'not_found' =>  __('Nothing found'),
        'not_found_in_trash' => __('Nothing found in Trash'),
        'parent_item_colon' => ''
    );
    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => array('slug' => 'event'),
        'has_archive' => 'event',
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => null,
        'supports' => array('title','editor','thumbnail'),
    ); 
    register_post_type( 'event' , $args );
}

我尝试过rewrite => truehas_archive => true,刷新重写规则,甚至为帖子类型注册分类法。 single-event.php工作正常。现在怎么办?

自定义帖子类型存档页面只需要两件事。

1(has_archive应该true

2(您需要在代码更新后刷新永久链接缓存一次。

函数.php

function my_custom_posts() {
    $labels = array(
        'name'               => _x( 'Events', 'post type general name' ),
        'singular_name'      => _x( 'Event', 'post type singular name' ),
        'add_new'            => _x( 'Add New', 'event' ),
        'add_new_item'       => __( 'Add New Event' ),
        'edit_item'          => __( 'Edit Event' ),
        'new_item'           => __( 'New Event' ),
        'all_items'          => __( 'All Events' ),
        'view_item'          => __( 'View Event' ),
        'search_items'       => __( 'Search Events' ),
        'not_found'          => __( 'No events found' ),
        'not_found_in_trash' => __( 'No events found in the Trash' ), 
        'parent_item_colon'  => '',
        'menu_name'          => 'Events'
    );
    $args = array(
        'labels'        => $labels,
        'description'   => 'Holds our events and event specific data',
        'public'        => true,
        'menu_position' => 5,
        'supports'      => array( 'title' ),
        'has_archive'   => true, // only this is required to enable archive page else 404 error will occur
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => array('slug' => 'events', 'with_front' => true),
        'capability_type' => 'post',
        'hierarchical' => false,
    );
    register_post_type( 'event', $args );   
    //flush_rewrite_rules();
}
add_action( 'init', 'my_custom_posts' );

默认archive.php将起作用,但如果您希望覆盖默认archive-<custom_post_slug>.php,则必须使用适当的存档模板,例如

archive-events.php

此外,如果您只是注册帖子类型,则需要刷新永久链接缓存。通过将永久链接结构更改为Wordpress管理来执行此操作。

现在您可以访问存档页面 https://domain/post_slug/

注意:如果您在永久链接结构中选择Numeric,网址将被 https://domain/archives/post_slug/

不确定这会回答您的问题,但是您是否重置了永久链接结构?

如果您刚刚添加了自定义帖子类型,则需要转到永久链接页面并按保存。

希望对您有所帮助!

看起来在您的

URL 示例中,您正在尝试获取实际的模板文件名本身。

但是,如果您已将 slug 定义为event那么您应该能够简单地访问 localhost/websitename/wordpress/event

我使用的是 CPT UI 插件,其中"有存档"标志默认设置为 False。您可以在以下位置更改此设置:

"Edit Post Type" tab > "Settings" > "Has Archive"

将其设置为 True,并且不要忘记刷新永久链接(单击永久链接页面上的保存(。

永久链接结构存在问题。我更改了设置 =>我的永久链接到"日期和名称"或"月份和名称"或"帖子名称",这解决了我的问题。