自定义后类型年度/月度存档 [英] Custom post type yearly/ monthly archive

查看:261
本文介绍了自定义后类型年度/月度存档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义职位类型新闻,在我的Word preSS网站。我公司采用先进的自定义字段插件元数据添加到每个岗位。

I have a custom post type "news" in my Wordpress site. I am using Advanced Custom Fields plugin to add meta data to each post.

我要创建的新闻项目数组​​作为存档:

I want to create an array of news items as an archive:

[2013]
    [January] => 5
[2012]
    [January] => 20
    [February] => 10
[2011]
    [April] => 30

我设法用得到这个工作:

I managed to get this working using:

    global $wpdb;
    $news = $wpdb->get_results(
        "SELECT wp_posts.post_date, COUNT(wp_posts.ID) as count
         FROM $wpdb->posts
         WHERE
         wp_posts.post_type = 'news' AND
         wp_posts.post_status = 'publish' AND
         wp_posts.post_date <= CURDATE() AND
         wp_posts.post_date >= DATE_SUB(CURDATE(), INTERVAL 3 YEAR)
         GROUP BY YEAR(wp_posts.post_date), MONTH(wp_posts.post_date)
         ORDER BY wp_posts.post_date DESC", 
         ARRAY_A);

    $archive = array();
    foreach ($news as $post):
        $year = date('Y', strtotime($post['post_date']));      
        $month = date('m', strtotime($post['post_date']));     
        $month_name = date('F', strtotime($post['post_date']));
        $post['url'] = 'NOT SURE ABOUT URL';
        $archive[$year][$month_name] = $post;
    endforeach;

我需要能够链接到特定年份和月份使用 http://example.com/2012/ 的http:// example.com/2012/10 /

由于这涉及到一个自定义文章类型新闻我不确定如何做到这一点?

As this involves a custom post type "news" I'm unsure how to make this happen?

推荐答案

为了做你需要,你需要修改的Word preSS重写才能够捕捉到年/月/等发布数据的自定义后的类型。

In order to do what you require, you need to modify Wordpress rewrites to be able to capture the year/month/etc post data for a custom post type.

您可以这样做,用下面的code:

You may do this with the following code:

/**
 * 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('news', $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;
}

您会发现,我有硬codeD 新闻 $规则= cpt_generate_date_archives(新闻,$ wp_rewrite) ; 的code部分。你可以改变这是必要的。

You will notice that I have hard-coded news in to the $rules = cpt_generate_date_archives('news', $wp_rewrite); portion of the code. You can change this as needed.

通过这个code,你应该能够浏览到 http://yoursite.com/news/2013/ 02 / 和接收档案的具体岗位类型为特定的时间上市。

With this code, you should be able to navigate to http://yoursite.com/news/2013/02/ and receive an archive listing for that specific post type for that specific time.

有关完整性,我将包括如何产生每月归档窗口小部件,以便利用这一点。

For completeness, I will include how to generate a monthly archive widget in order to utilize this.

/**
 * 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;
}

要使用此功能,只需提供自定义职位类型的蛞蝓,即: get_cpt_archives(新闻)。这将返回独特的年/日期/链接阵列,即:

To use this function just supply the slug of the custom post type, ie: get_cpt_archives( 'news' ). This will return an array of unique Year/Dates/Links, ie:

Array
(
    [0] => Array
        (
            [month] => February
            [year] => 2013
            [link] => http://yoursite.com/news/2013/02
        )

    [1] => Array
        (
            [month] => January
            [year] => 2013
            [link] => http://yoursite.com/news/2013/01
        )

)

您可以通过这些循环使用的foreach 和输出它们不过你想要的。

You can loop through these with a foreach and output them however you want.

另外,你可以使用 get_cpt_archives(新闻,真正的)将自动回声包裹在一个&其中每个项目;李&GT; 链接到其指定的归档。

Alternatively, you can use get_cpt_archives( 'news', true ) which will automatically echo each item wrapped in an <li> linking to its specific archive.

输出的格式不正是你想要的,所以你将不得不调整它有点让它在显示

The formatting of the output is not exactly what you wanted, so you will have to tweak it a bit to get it to display in the

Year
    Month
    Month
Year
    Month

这是您所需要的格式。

我希望这有助于。

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

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