Support » Plugins and Hacks » Only Show Ads on Old Posts

  • 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. 🙁

Viewing 4 replies - 1 through 4 (of 4 total)
  • Great idea. I don’t know but maybe this helps
    http://us2.php.net/strtotime

    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).

    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!

    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…

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Only Show Ads on Old Posts’ is closed to new replies.