How to make a custom Archive Page

You can get the queried date from the query vars in the $wp_query object:

if( is_date() ){
    if( isset( $wp_query->query_vars['year'] ) ){
        echo 'queried year is ' . $wp_query->query_vars['year'];
    }
    if( isset( $wp_query->query_vars['monthnum'] ) ){
        echo 'queried month is ' . $wp_query->query_vars['monthnum'];
    }
    if( isset( $wp_query->query_vars['day'] ) ){
        echo 'queried day is ' . $wp_query->query_vars['day'];
    }
}

or just via get_query_var():

echo get_query_var( 'year' );

Does that answer your question?

EDIT

You can check if the site is using pretty permalinks by checking for the presence of the permalink structure option. In that case, you can fetch and parse the m query var instead of year / monthnum / day:

if( get_option('permalink_structure') ){
    // using pretty permalinks
} else {
    // not using pretty permalinks
    // note that month or day may be empty
    // test for their existence before trying to use!
    $year = substr( get_query_var( 'm' ), 0, 4);
    $month = substr( get_query_var( 'm' ), 4, 2);
    $day = substr( get_query_var( 'm' ), 6, 2);
}

Leave a Comment