自定义帖子类型的日期档案 [英] Date archives for custom post type

查看:97
本文介绍了自定义帖子类型的日期档案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到许多与此相关的问题/帖子,但尚未找到合适的解决方案。基本上,我试图做 wp_get_archives 的工作,但是对于自定义帖子类型(个人而言,我不确定为什么 wp_get_archives 不这样做'不支持自定义帖子类型!)。



我当前使用的代码如下



functions.php

  function Cpt_getarchives_where_filter($ where,$ r){
$ post_type ='事件;
return str_replace( post_type ='post', post_type ='$ post_type',$ where);
}

sidebar-events.php

  add_filter('getarchives_where','Cpt_getarchives_where_filter',10,2); 
wp_get_archives();
remove_filter('getarchives_where','Cpt_getarchives_where_filter',10);

此代码显示日期(例如2014年4月,2014年3月)等,很好,但是单击链接只会转到404。在每个日期链接上创建的URL是/ 2014/04 /,但是应该类似于/ events / 2014/04/。



是否可以在URL中包含事件,以便可以使用archive-events.php模板,并且是否有任何原因导致链接当前生成404?



非常感谢您的帮助

解决方案

互联网上有很多与您类似的示例,但是问题出在尽管 wp_get_archive()会创建一个自定义帖子类型档案列表,但链接仍指向默认帖子类型。这是因为Wordpress不会为自定义帖子类型的档案生成重写规则,因此您必须手动创建它们。这是一个如何为年度,每月和每日存档生成规则的示例。它还显示了如何使用 get_archives_link 过滤器转换链接。确保添加'has_archive'=> true register_post_type()参数数组,并通过访问 settings->永久链接刷新刷新规则

functions.php

  add_filter('getarchives_where','getarchives_where_filter',10,2); 
add_filter(‘generate_rewrite_rules’,‘generate_events_rewrite_rules’);

函数getarchives_where_filter($ where,$ args){

if(isset($ args ['post_type']))} {
$ where = WHERE post_type ='$ args [post_type]'AND post_status ='publish';
}

返回$ where;
}

函数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->规则= $ event_rules + $ wp_rewrite->规则;
}

函数get_archives_events_link($ link){

return str_replace(get_site_url(),get_site_url()。‘/ events’,$ link);

};

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


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!).

The code I am currently using is as follows

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

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 (e.g. 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/.

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?

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. This is because Wordpress do not generate rewrite rules for the archives of the custom post type, you will have to manually create them. 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. 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.

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

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

这篇关于自定义帖子类型的日期档案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆