[英]Date archives for custom post type
I have seen many questions/posts regarding this, but have yet to find a decent solution. 我已经看到很多关于此的问题/帖子,但还没有找到一个像样的解决方案。 Basically I am trying to do what wp_get_archives
does, but for a custom post type (personally I am unsure why wp_get_archives
doesn't support custom post types!). 基本上我正在尝试做wp_get_archives
所做的事情,但对于自定义帖子类型(个人我不确定为什么wp_get_archives
不支持自定义帖子类型!)。
The code I am currently using is as follows 我目前使用的代码如下
functions.php 的functions.php
function Cpt_getarchives_where_filter( $where , $r ) {
$post_type = 'events';
return str_replace( "post_type = 'post'" , "post_type = '$post_type'" , $where );
}
sidebar-events.php 侧边栏events.php
add_filter( 'getarchives_where' , 'Cpt_getarchives_where_filter' , 10 , 2 );
wp_get_archives();
remove_filter('getarchives_where' , 'Cpt_getarchives_where_filter' , 10 );
This code displays the dates (eg April 2014, March 2014) etc, which is great, but clicking the links just goes to a 404. The URL that is created on each date link is /2014/04/, however it should be something like /events/2014/04/. 此代码显示日期(例如2014年4月,2014年3月)等,这很棒,但点击链接只会转到404.在每个日期链接上创建的URL是/ 2014/04 /,但它应该是某些东西喜欢/ events / 2014/04 /。
Is there any way to include 'events' in the URL so that the archive-events.php template can be used, and is there any reason why the links currently generate a 404? 有没有办法在URL中包含'events',以便可以使用archive-events.php模板,是否有任何理由说明链接当前生成404?
Many thanks for any help 非常感谢任何帮助
There are many examples on the Internet similar as your, but the problem is that although wp_get_archive()
will create a list of custom post type archives, the links still points to the default post type. 互联网上有很多类似你的例子,但问题是虽然wp_get_archive()
会创建一个自定义帖子类型档案列表,但链接仍然指向默认的帖子类型。 This is because Wordpress do not generate rewrite rules for the archives of the custom post type, you will have to manually create them. 这是因为Wordpress不会为自定义帖子类型的存档生成重写规则,您必须手动创建它们。 Here is an example of how to generate rules for yearly, monthly, and daily archives. 以下是如何为年度,月度和日常归档生成规则的示例。 It also shows how to convert links with get_archives_link
filter. 它还显示了如何使用get_archives_link
过滤器转换链接。 Be sure to add 'has_archive' => true
to register_post_type()
array of arguments and to flush the rewrite rules by visiting the settings->permalinks
page in admin. 确保将'has_archive' => true
添加到register_post_type()
参数数组中,并通过访问admin中的settings->permalinks
页面来刷新重写规则。
functions.php 的functions.php
add_filter( 'getarchives_where', 'getarchives_where_filter', 10, 2 );
add_filter( 'generate_rewrite_rules', 'generate_events_rewrite_rules' );
function getarchives_where_filter( $where, $args ) {
if ( isset($args['post_type']) ) {
$where = "WHERE post_type = '$args[post_type]' AND post_status = 'publish'";
}
return $where;
}
function generate_events_rewrite_rules( $wp_rewrite ) {
$event_rules = array(
'events/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$' => 'index.php?post_type=events&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]',
'events/([0-9]{4})/([0-9]{1,2})/?$' => 'index.php?post_type=events&year=$matches[1]&monthnum=$matches[2]',
'events/([0-9]{4})/?$' => 'index.php?post_type=events&year=$matches[1]'
);
$wp_rewrite->rules = $event_rules + $wp_rewrite->rules;
}
function get_archives_events_link( $link ) {
return str_replace( get_site_url(), get_site_url() . '/events', $link );
};
sidebar.php examples sidebar.php示例
add_filter( 'get_archives_link', 'get_archives_events_link', 10, 2 );
wp_get_archives( array( 'post_type' => 'events' ) );
wp_get_archives( array( 'post_type' => 'events', 'type' => 'yearly' ) );
wp_get_archives( array( 'post_type' => 'events', 'type' => 'monthly' ) );
wp_get_archives( array( 'post_type' => 'events', 'type' => 'daily' ) );
remove_filter( 'get_archives_link', 'get_archives_events_link', 10, 2 );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.