开发者

Wordpress custom post type yearly/monthly archives

开发者 https://www.devze.com 2023-01-17 11:14 出处:网络
Is there anyway to have yearly or monthly archives with custom post types in wordpress? I\'ve searched far and wide to no开发者_JAVA百科 avail.

Is there anyway to have yearly or monthly archives with custom post types in wordpress? I've searched far and wide to no开发者_JAVA百科 avail. I would imagine the url structure to be this:

http://sitename/custom-post-type/year/month

Anyone?


My plugin Custom Post Permalinks lets you tweak the permalink settings. However, as of now, it only works on non-hierarchical post types. I'm working on a new release that will allow both types.


You can try this code. Replace CPT in array with your CPT name.

This function will convert the the following URL

http://example.net/CPT/2016/06

to

http://example.net/?post_type=CPT&m=201606

which is actually used by wordpress.

add_action('init', 'date_cpt_rewrites');

function date_cpt_rewrites() {
    $custom_post_types = array('CPT'); //some example post types
    foreach ( $custom_post_types as $post_type ) {
        $rule = '^' . $post_type . '/(\d+)/(\d+)/?$';
        $rewrite = 'index.php?post_type=' . $post_type . '&m=$matches[1]$matches[2]';
        add_rewrite_rule($rule,$rewrite,'top');
    }


}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号