admin管理员组

文章数量:1122846

I would like to change archives.php post listing as follows. Whenever clicked on certain link in archives select drop down box, archive page should list posts from May - Dec of that year, plus Jan - April of next year.

Eg. If i clicked on archives drop down, 2015...Archives page /2015/ will list all posts from the database starting from May 2015 to December 2015, also January 2016 to April 2016. This is to display posts in an annual year in the calendar.

My current wp archive have posts code like below;

// Start the Loop.
        while ( have_posts() ) : the_post();

            /*
             * Include the Post-Format-specific template for the content.
             * If you want to override this in a child theme, then include a file
             * called content-___.php (where ___ is the Post Format name) and that will be used instead.
             */
            get_template_part( 'content', get_post_format() );

        // End the loop.
        endwhile;

I would like to change archives.php post listing as follows. Whenever clicked on certain link in archives select drop down box, archive page should list posts from May - Dec of that year, plus Jan - April of next year.

Eg. If i clicked on archives drop down, 2015...Archives page http://domainname.xyz/2015/ will list all posts from the database starting from May 2015 to December 2015, also January 2016 to April 2016. This is to display posts in an annual year in the calendar.

My current wp archive have posts code like below;

// Start the Loop.
        while ( have_posts() ) : the_post();

            /*
             * Include the Post-Format-specific template for the content.
             * If you want to override this in a child theme, then include a file
             * called content-___.php (where ___ is the Post Format name) and that will be used instead.
             */
            get_template_part( 'content', get_post_format() );

        // End the loop.
        endwhile;
Share Improve this question edited Apr 9, 2016 at 11:41 yjs asked Apr 9, 2016 at 10:21 yjsyjs 515 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

It depends on how your archives.php is designed.

You can use built-in wp_get_archives() function to get posts for last 12 months:

<?php
wp_get_archives(
    array(
        'type' => 'monthly',
        'limit' => 12
    )
);

as described in the Codex.

Or you can modify the WP_Query using 'date_query' parameter:

<?php
$args = array(
    'date_query' => array(
        array(
            'after'     => strtotime("-1 year"), // a year before
            'before'    => strtotime("now");, // now
            'inclusive' => true, // false to exclude first and last day
        )
    )
);
$query = new WP_Query( $args );

as also described there.

本文标签: How can I change archivephp posts display