WordPress.org

Ready to get started?Download WordPress

Forums

Only Show Ads on Old Posts (5 posts)

  1. jeremywright
    Member
    Posted 8 years ago #

    Hey, this is kicking my ass. I'm trying to make it only show an ad if the post is older than 30 days old. In an ideal world it would ALSO only show the ad if the user is NOT logged in... But I'd settle for just the 30 days bit. Right now it's always showing the ad.

    Here's the code I've got.

    <?php
    if(time() >= strtotime("-30 days")) {
    echo("<Span name=KonaBody>");
    the_content(__('(<p class="serif">Read the rest of this entry &raquo;</p>)'));
    echo("</span>");
    } else {
    the_content(__('(<p class="serif">Read the rest of this entry &raquo;</p>)'));
    }
    ?>

    I feel like an idiot. Really. :(

  2. Dgold
    Member
    Posted 8 years ago #

    Great idea. I don't know but maybe this helps
    http://us2.php.net/strtotime

  3. Kafkaesqui

    Posted 8 years ago #

    Simplest way to do this might be to change the code before your first echo to:

    <?php
    global $user_identity;
    $days_since = floor((date('U') - get_the_time('U')) / 86400);
    if(!$user_identity || ($days_since >= 30)) {

    Notes: The global scope makes WP's $user_identity var available (if it isn't), which is only in the air if a user is logged in. The $days_since is a bit complicated, but it: 1. grabs the current (server) date/time and a post's date/time in the UNIX date format (seconds since 1.1.1970), 2. minuses post from current, 3. divides that by number of seconds in a day, and 4. uses PHP floor() to round that value down to an integer (i.e. day).

  4. jeremywright
    Member
    Posted 8 years ago #

    Kafkaesqui: Fantastic, thanks, that's 10 times better! The only thing is the !user_identity bit doesn't seem to be working, as I see the ads even on older posts (though they are NOT showing on new posts, which is great!).

    Was wondering if the pipes shouldn't be ampersands (ie: not logged in AND older than 30 days)? But that didn't change the ads showing up...

    Anyways, this is already 10 times better, so thank you!

  5. Kafkaesqui

    Posted 8 years ago #

    Um, oops. Actually you're right. It should be:

    if(!$user_identity && ($days_since >= 30)) {

    Because you do want both conditions to be true before including your span/ad/whatever. Last non-tested code posting for me for awhile...

Topic Closed

This topic has been closed to new replies.

About this Topic