archive.php 不起作用,WordPress自定义帖子类型archive-.php无法正常工作

此篇博客介绍了如何在WordPress中创建一个名为'事件'的自定义帖子类型,配置其标签、属性,并详细讲解了URL重写规则的设置,以便实现如example.com/events/2013/07/01的访问方式。同时提醒读者注意存档模板和刷新永久链接缓存。
摘要由CSDN通过智能技术生成

好的,我和你的问题类似,现在我相信我已经解决了.

我不确定我的代码会准确反映你需要做什么,但它应该很容易适应.请注意归档URL的重写规则.

的functions.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,

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

add_action('init', 'event_archive_rewrite');

function event_archive_rewrite(){

add_rewrite_rule('^events/([0-9]{4})/([0-9]{2})/?','index.php?post_type=event&year=$matches[1]&monthnum=$matches[2]','top');

add_rewrite_rule('^events/([0-9]{4})/?','index.php?post_type=event&year=$matches[1]','top');

}

这现在允许我访问这样的事件:

example.com/events/2013/07/01

请记住准备好适当的存档模板,例如

archive-event.php

此外,如果您只是注册帖子类型,则需要刷新永久链接缓存.通过进入WordPress管理员并再次“保存”永久链接结构来做到这一点.

真的希望这有助于某人!

谢谢,米奇.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值