TransWikia.com

How can I change archive.php posts display?

WordPress Development Asked by yjs on November 19, 2021

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;

One Answer

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.

Answered by Max Yudin on November 19, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP