WordPress.org

Ready to get started?Download WordPress

Forums

Post Expirator
Display expiration outside post/page within template files. (7 posts)

  1. Luke Etheridge
    Member
    Posted 1 year ago #

    Hi there,

    Great plugin btw.

    Just wondering how I can display the post expiration within my template files as obviously the shortcode won't work for that.

    I'd like to display as 'days left' also. So, '4 Days Left' for example.

    Thanks.
    Luke

    http://wordpress.org/plugins/post-expirator/

  2. Luke Etheridge
    Member
    Posted 1 year ago #

    I got how to add the short code with...

    <?php echo do_shortcode('[postexpirator]'); ?>

    But now just need to know how to display as 'Days Left'?

    Thanks.
    Luke

  3. Luke Etheridge
    Member
    Posted 1 year ago #

    Alsoooo...

    This is my code...

    <span class="expire"><?php echo do_shortcode('[postexpirator]'); ?></span>

    BUT, how do I have this only display when 'Enable Post Expiration' is checked for that specific post/post type?

    -----

    So if 'Enable Post Expiration' is checked display this...

    <span class="expire"><?php echo do_shortcode('[postexpirator]'); ?></span>

    If unchecked display nothing.

  4. Luke Etheridge
    Member
    Posted 1 year ago #

    OK so for the above ^ I'm doing...

    <?php if ( $expirationdatets ) { ?><span class="expire"><?php echo do_shortcode('[postexpirator]'); ?></span><?php } ?>

    Let me know if there is a better way please?

    The only question outstanding is how to display the expiration as '% Days Left'.

    Thanks.
    Luke

  5. Luke Etheridge
    Member
    Posted 1 year ago #

    Right, it appears I jumped the gun and I'm in fact my own support :D

    I've fixed all that I needed with the following...

    <?php
       $expirationdatets = get_post_meta($post->ID, '_expiration-date', true);
       $postid = $wp_query->post->ID;
       $date=get_post_meta($post->ID, '_expiration-date', true);
       $diff=$date-time();
       $days=floor($diff/(60*60*24));
    ?>

    And...

    <?php if ( $expirationdatets ) { ?>
    	<span class="expire" title="Offer Expiry">
    		<?php if ($days == '-15931') {
    		  echo ""; }
    		else if ($days == '0') {
    		  echo "<b>Expires within 24hrs</b>"; }
    		else if ($days == '1') {
    		  echo "<b>$days Day Left</b>"; }
    		else if ($days == '2') {
    		  echo "<b>$days Days Left</b>"; }
    		else if ($days == '3' || $days == '4' || $days == '5' || $days == '6' || $days == '7') {
    		  echo "$days Days Left"; }
    		else {
    		  echo do_shortcode('[postexpirator]'); }
    		?>
    	</span>
    <?php } ?>

    Please let me know if there is a better/cleaner way to achieve exactly that of the above, as you can probably see my PHP is not at it's best ;)

    Thanks,
    Luke

  6. Mediamojo13
    Member
    Posted 1 year ago #

    Hi Luke, you seem like a genius!

    Should I insert this code into the page template? Where did you place your code? Have you got a visual example as well?

    Love your work!

  7. Luke Etheridge
    Member
    Posted 1 year ago #

    Hey Mediamojo13,

    Thanks for the kind words!

    Nah definitely no genius - not in PHP anyway ;)

    All you have to do is insert the above code inside the loop of your template file and away you go. This will simply display the following...

    1 > 'Expires within 24hrs' (wrapped in <b>)
    2 > '1 Day Left' (wrapped in <b>)
    3 > '2 Days Left' (wrapped in <b>)
    4-7 > '% Days Left'
    7 < '24th October 2013' (normal postexpirator display)

    I'll give you a visual when it's live but it may not be for a month or so.

    Let me know if you have any problems.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic