WordPress.org

Ready to get started?Download WordPress

Forums

Conditional: for posts older than x weeks (7 posts)

  1. Jiiinx
    Member
    Posted 7 years ago #

    I've been dabbling with wordpress for the last few months and I must say I've always found it very easy to customize, and maintain...

    But today I ran into the first problem I couldn't solve by reading the codex or searching these support forums... Here goes:

    I want to change the template for posts (single.php) older than a certain amount of weeks, days or months... Let's say I want to display a certain text above all posts older than 25 days....

    Is that possible? I saw the date conditionals, but I couldn't find a time difference conditional? Can this be achieved by a workaround?

    Thanks in advance!

  2. 2notch
    Member
    Posted 7 years ago #

    You may be able to rip some code from Ashwin Bihari’s old Auto-Delete- Posts plugin to get what you need. I have updated the plugin to work with WP 2.1.2 and you can get it here:
    http://digitalwestex.org/2007/03/23/auto-delete-posts-for-wordpress/

  3. Jiiinx
    Member
    Posted 7 years ago #

    Thanks 2notch;

    I've been working with the plugin trying to get it to move but I've actually not moved an inch towards actually getting the single.php file to do something different for posts older than a certain days.

    I'm afraid I'm missing the obvious here :(

  4. Glenn Ansley
    Member
    Posted 7 years ago #

    Couldn't you make a conditional statement around the post within the loop?

    If date is more than 25 days from today, do this. Otherwise, do this.

  5. Jiiinx
    Member
    Posted 7 years ago #

    That's the idea, yes. But how do I make this conditional statement, the statement can't be found in the conditionals list :(

    Anyone?

  6. Glenn Ansley
    Member
    Posted 7 years ago #

    Try this:

    <?php
    	$today = date("r");
    	$articledate = get_the_time('r');
    	$difference = round((strtotime($today) - strtotime($articledate))/(24*60*60),0);
    
                if ($difference >= 25) {
                   Do Stuff Here
                }else{
                   Do Stuff Here
                }
    ?>

    That should 1) set today's date, 2) set the post's date, 3) find the difference, 4) Create a conditional based on the difference in days between today and the post date.

    The conditional and the initial variables need to be executed within the loop.

  7. Jiiinx
    Member
    Posted 7 years ago #

    Blepoxp, thank you so much. This is working like a charm! Great! Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic