将日期档案添加到自定义邮政类型

时间:2014-04-04 作者:Stewarty

URL重写有点问题。

尝试获取以下URL以显示2014年的所有布道

http://example.com/sermons/2014
目前,它正在寻找一个名为2014年的布道

function caffeine_rewite_rules(){
    add_rewrite_rule("sermons/([0-9]{4})/?$", \'index.php post_type=ctc_sermon&year=$matches[1]\', "top");
}
add_action( \'plugins_loaded\', \'caffeine_rewite_rules\');
这“应该”在重写数组的顶部添加一个新的重写规则,该规则与布道/年匹配,但它从不匹配。有什么想法吗?

2 个回复
SO网友:Eric Holmes

我相信这会帮你解决的。包括每月和每日。如果定义了不同于自定义post类型slug的重写slug,它也会捕获。

/* This function handles setting up Date archive rewrite rules for
 * ANY custom post type - You pass the CPT, and it will use the
 * re-written slug if applicable.
 */
function eh_generate_date_archives( $cpt ) {
    global $wp_rewrite;
    $rules = array();
    $post_type = get_post_type_object( $cpt );
    $slug_archive = $post_type->has_archive;
    if ( $slug_archive === false ) return $rules;
    if ( $slug_archive === true ) {
        $slug_archive = $post_type->rewrite[\'slug\'] ? $post_type->rewrite[\'slug\'] : $post_type->name;
    }
    $dates = array(
        array(
            \'rule\' => "([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})",
            \'vars\' => array( \'year\', \'monthnum\', \'day\' )
        ),
        array(
            \'rule\' => "([0-9]{4})/([0-9]{1,2})",
            \'vars\' => array( \'year\', \'monthnum\' )
        ),
        array(
            \'rule\' => "([0-9]{4})",
            \'vars\' => array( \'year\' )
        )
      );
    foreach ($dates as $data) {
        $query = \'index.php?post_type=\'.$cpt;
        $rule = $slug_archive.\'/\'.$data[\'rule\'];

        $i = 1;
        foreach ( $data[\'vars\'] as $var ) {
            $query.= \'&\'.$var.\'=\'.$wp_rewrite->preg_index($i);
            $i++;
        }
        $rules[$rule."/?$"] = $query;
        $rules[$rule."/feed/(feed|rdf|rss|rss2|atom)/?$"] = $query."&feed=".$wp_rewrite->preg_index($i);
        $rules[$rule."/(feed|rdf|rss|rss2|atom)/?$"] = $query."&feed=".$wp_rewrite->preg_index($i);
        $rules[$rule."/page/([0-9]{1,})/?$"] = $query."&paged=".$wp_rewrite->preg_index($i);
    }
    return $rules;
}

SO网友:Faye

重要修订

我将这篇文章留在这里,因为其他人一定会想到这一点,并偶然发现它!正如下面的评论所指出的,这将获取当前年份,并且在永久链接刷新时会更改所有URL。这当然不是我想要的!这就是为什么下面的方法不是一个好主意。谢谢米洛的提醒。我甚至没有想到这一点。

我找到了一种非常简单的方法,我有点担心这肯定是非常错误的,否则就不会被这么多人忽视。我知道这是一篇老帖子,但我今天在寻找这个问题的答案时找到了它。

同样,我也制作了自己的帖子类型。

在我的post类型的参数(在本例中为events)中,我使用了可用于自定义post类型的重写。在自定义post类型之前,我添加了一个年度变量。

$year = date("Y");

  $events_labels = array(
    \'name\'                => _x( \'Events\', \'Post Type General Name\', \'reaction\' ),
    \'singular_name\'       => _x( \'Event\', \'Post Type Singular Name\', \'reaction\' ),
    \'menu_name\'           => __( \'Events\', \'reaction\' ),
    \'name_admin_bar\'      => __( \'Events\', \'reaction\' ),
    \'parent_item_colon\'   => __( \'Parent:\', \'reaction\' ),
    \'all_items\'           => __( \'All Events\', \'reaction\' ),
    \'add_new_item\'        => __( \'Add New Event\', \'reaction\' ),
    \'add_new\'             => __( \'Add New\', \'reaction\' ),
    \'new_item\'            => __( \'New\', \'reaction\' ),
    \'edit_item\'           => __( \'Edit\', \'reaction\' ),
    \'update_item\'         => __( \'Update\', \'reaction\' ),
    \'view_item\'           => __( \'View\', \'reaction\' ),
    \'search_items\'        => __( \'Search Events\', \'reaction\' ),
    \'not_found\'           => __( \'No Events found\', \'reaction\' ),
    \'not_found_in_trash\'  => __( \'No Events found in Trash\', \'reaction\' ),
  );

  $events_args = array(
    \'label\'               => __( \'events\', \'reaction\' ),
    \'description\'         => __( \'YHQ Events\', \'reaction\' ),
    \'labels\'              => $events_labels,
    \'supports\'            => array( \'title\', \'editor\', \'author\', \'thumbnail\', \'revisions\', ),
    \'hierarchical\'        => false,
    \'public\'              => true,
    \'show_ui\'             => true,
    \'show_in_menu\'        => true,
    \'menu_position\'       => 5,
    \'menu_icon\'           => \'dashicons-calendar\',
    \'show_in_admin_bar\'   => true,
    \'show_in_nav_menus\'   => true,
    \'can_export\'          => true,
    \'has_archive\'         => false,
    \'exclude_from_search\' => false,
    \'publicly_queryable\'  => true,
    \'rewrite\'            => array( \'slug\' => \'events/item/\' . $year . \'\'),
    \'capability_type\'     => \'page\',
  );
你可以看到我的slug重写数组包括一个额外的/项/路径(不需要,只是因为我需要为我们的目的做更多的自定义,它可以帮助其他人),然后是我的年度变量。

这太简单了,我在等另一只鞋掉下来!如果有人知道为什么这是个糟糕的主意,请告诉我。

结束

相关推荐