简体   繁体   中英

Custom Post Type monthly archive

I have the following code in my functions.php:

function add_rewrite_rules($aRules) {
    $aNewRules = array(
        'news/([0-9]{4})/page/?([0-9]{1,})/?$' => 'index.php?post_type=news&year=$matches[1]&paged=$matches[2]',
        'news/([0-9]{4})/?$' => 'index.php?post_type=news&year=$matches[1]',
        'blog/([0-9]{4})/page/?([0-9]{1,})/?$' => 'index.php?post_type=blog&year=$matches[1]&paged=$matches[2]',
        'blog/([0-9]{4})/?$' => 'index.php?post_type=blog&year=$matches[1]'
    );
    $aRules = $aNewRules + $aRules;
    return $aRules;
}

add_filter('rewrite_rules_array', 'add_rewrite_rules');

This has made my permalinks work like this:

/blog

/blog/post-name

/blog/2014

I don't understand the code well enough to adapt it to do monthly archives like:

/blog/2014/01

Try adding

 'blog/([0-9]{4})/([0-9]{1,2})/?$' => 'index.php?post_type=blog&year=$matches[1]&monthnum=$matches[2]'

to your array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM