按月分组的自定义分类的存档页面

时间:2015-07-13 作者:Blue Rose

我正在处理自定义帖子类型的自定义分类法的归档页面。我需要按特定分类法的月份显示档案。

用于按月检索特定分类中的帖子。然而,我使用了以下代码。

function my_custom_post_archive_where($where,$args){  
    $post_type  = isset($args[\'post_type\'])  ? $args[\'post_type\']  : \'post\';  
    $where = "WHERE post_type = \'$post_type\' AND post_status = \'publish\'";
    return $where;  
}

add_filter( \'getarchives_where\',\'my_custom_post_archive_where\',10,2); 
但上面的过滤器给出了特定帖子类型中的所有帖子。我只需要在特定的分类法和它各自的页面上发表文章。

例如:

档案:

2015年7月-2015年6月

我该怎么做?请任何人建议我,因为我真的被困在这里了。

提前感谢:)。

1 个回复
SO网友:Blue Rose

嘿,不管怎样,我找到了一个确切的答案,这个答案很有魅力。我希望这将有助于某人,所以我把它张贴在这里。

首先,您需要在函数中添加以下代码。php

/**
 * Custom post type date archives
 */

/**
 * Custom post type specific rewrite rules
 * @return wp_rewrite             Rewrite rules handled by Wordpress
 */
function cpt_rewrite_rules($wp_rewrite) {
    $rules = cpt_generate_date_archives(\'your-post-type\', $wp_rewrite);
    $wp_rewrite->rules = $rules + $wp_rewrite->rules;
    return $wp_rewrite;
}
add_action(\'generate_rewrite_rules\', \'cpt_rewrite_rules\');

/**
 * Generate date archive rewrite rules for a given custom post type
 * @param  string $cpt        slug of the custom post type
 * @return rules              returns a set of rewrite rules for Wordpress to handle
 */
function cpt_generate_date_archives($cpt, $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->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;
}


/**
 * Get a montlhy archive list for a custom post type
 * @param  string  $cpt  Slug of the custom post type
 * @param  boolean $echo Whether to echo the output
 * @return array         Return the output as an array to be parsed on the template level
 */
function get_cpt_archives( $cpt, $echo = false )
{
    global $wpdb; 
    $sql = $wpdb->prepare("SELECT * FROM $wpdb->posts WHERE post_type = %s AND post_status = \'publish\' GROUP BY YEAR($wpdb->posts.post_date), MONTH($wpdb->posts.post_date) ORDER BY $wpdb->posts.post_date DESC", $cpt);
    $results = $wpdb->get_results($sql);

    if ( $results )
    {
        $archive = array();
        foreach ($results as $r)
        {
            $year = date(\'Y\', strtotime( $r->post_date ) );
            $month = date(\'F\', strtotime( $r->post_date ) );
            $month_num = date(\'m\', strtotime( $r->post_date ) );
            $link = get_bloginfo(\'siteurl\') . \'/\' . $cpt . \'/\' . $year . \'/\' . $month_num;
            $this_archive = array( \'month\' => $month, \'year\' => $year, \'link\' => $link );
            array_push( $archive, $this_archive );
        }

        if( !$echo )
            return $archive;
        foreach( $archive as $a )
        {
            echo \'<li><a href="\' . $a[\'link\'] . \'">\' . $a[\'month\'] . \' \' . $a[\'year\'] . \'</a></li>\';
        }
    }
    return false;
}
编辑此行$rules=cpt\\u generate\\u date\\u archives(\'your-post-type\',$wp\\u rewrite);

更改帖子类型,然后在要显示帖子类型存档的页面上按如下方式调用

<?php get_cpt_archives( \'your-post-type\', true ); ?>
如果要删除格式(如li),只需按以下方式使用即可:

<?php get_cpt_archives( \'your-post-type\' ); ?>
不要忘记,您必须创建这样的归档页面

archive-{your-post-type}.php
就是这样。快乐编码:)

结束

相关推荐