• Hi

    I am writing a series of posts that I would like displayed annually. i.e. Instead of a normal archive on the front page to only show posts for that date. It will still be possible to find other posts through category, so it is simply to show posts for that date. Secondly, is there some way to make posts repeat annually please?

    Thanks for any help.

    Rich

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter Richard Brown

    (@cregy)

    Hi

    I have found this code snippet but I think it disaplays all posts for the 24th, e.g. Jan 24th, Feb 24th, etc.

    <?php
    $day = date('j');
    query_posts('day='.$day);
    if (have_posts()) :
       while (have_posts()) : the_post();
    ?>
    <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="view: <?php the_title(); ?>"><?php the_title(); ?></a></h3>
    <div class="storycontent"><?php the_content(); ?></div>
    <?php endwhile; ?> <?php endif; ?>

    Is it possible to amend this slightly please?

    Does anyone know of a plugin please? Or is this done at theme stage?

    Thanks

    Rich

    Moderator bcworkz

    (@bcworkz)

    So you have at least one post for every day of the year and you want that day’s posts to be shown, regardless of year? So a post from Aug 18 2016 will be shown on Aug 18 of every year on forward? You just need different query_posts() arguments.

    $args = array(
    	'date_query' => array(
    		array(
    			'day' => date( 'j' ),
    			'month' => date( 'n' ),
    		),
    	),
    );
    query_posts( $args );

    You probably don’t care, but for anyone else reading who might, the “date_query” argument is preferred over the older individual date arguments. It allows extremely complex date logic to be applied to the query. Multiple argument arrays can be added to the one overall date_query array. Inner arrays can be nested for even more complex logic.

    The use of query_posts() is now frowned upon, it can be very inefficient. But without knowing how you want this query applied in your site, I can’t suggest a better solution. The preferred approach is to alter the default query through the “pre_get_posts” action, but to do so we need to know when to apply the change and when not to.

    Thread Starter Richard Brown

    (@cregy)

    Hi

    Thanks for the very helpful reply. I will have to research some of the information you have given me.

    Every Day

    The above is the site I am applying it to. Does this help to answer your query in terms of when to apply the change please?

    Thanks

    Rich

    Moderator bcworkz

    (@bcworkz)

    So you want your home page to only display posts made on the same day of the year as the current day, so for Aug 18, display all Aug 18 posts from 2018, 2017, 2016, 2015, etc.?

    If we’re going to do things the “right” way, first of all, you shouldn’t be directly altering theme templates (unless you made your own theme). Your changes will be lost when the theme is updated. To customize a theme, create a child theme. Because we are going to alter the posts query via the pre_get_posts action, no template alterations are required, so this could also be accomplished via a custom plugin instead of child theme.

    To alter the home page query, add the following to functions.php of your child theme, or the main code page of a custom plugin:

    add_action('pre_get_posts', 'cgy_same_day');
    function cgy_same_day( $query ) {
      if ( $query->is_home() && $query->is_main_query()) {
        $query->set('date_query', array(
    	array(
    	  'day' => date( 'j' ),
    	  'month' => date( 'n' ),
    	),
        ));
      }
      return;
    }

    No need to alter any theme templates this way ??

    Thread Starter Richard Brown

    (@cregy)

    Sorry for the long delay in coming back to you. I’ve not been well and have been trying to work with someone to turn this into a plugin.

    Thanks for all the help.

    Thread Starter Richard Brown

    (@cregy)

    I have finally built the site using your code. Thank you so much for all your help.

    If anyone wants to look…. I am running it here:
    https://gucu.org.uk/everydaywithjesus/

    Rich

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Topics displayed by date’ is closed to new replies.