• Hi,

    I have a problem of Displaying custom posts – only if custom meta box date is not expired. I can’t figure out the where I should put the if statement in the code. My idea is;

    if($daysleft >= 0)
    then execute the while “custom post”;

    The following code is displaying the all the custom posts both – expired and not expired.

    ?php
        while(have_posts() ) : the_post();
        $thumb = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
        $discount = get_post_meta($post->ID, '_single_discount', true);
        $date = get_post_meta($post->ID, '_single_date', true);
        $code = get_post_meta($post->ID, '_single_code', true);
        $url = get_post_meta($post->ID, '_single_url', true);
        $daysleft = round( ($date-time()) / 24 / 60 / 60);
    ?>
    <div class="coupon-date">
        <?php
        if($date == '')_e('Х?чинтэй', 'Couponize');
        else if($daysleft <= 0) _e('Дууссан', 'Couponize');
        else echo sprintf( _n('%d day left.', '%d Хоног ?лдсэн.', $daysleft, 'Couponize'), $daysleft );
    	?>
    </div>
    <?php endwhile; ?>

    Thanx in advance,
    Anandmongol

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi,

    Please try the following.

    <?php
        while(have_posts() ) : the_post();
        $thumb = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
        $discount = get_post_meta($post->ID, '_single_discount', true);
        $date = get_post_meta($post->ID, '_single_date', true);
        $code = get_post_meta($post->ID, '_single_code', true);
        $url = get_post_meta($post->ID, '_single_url', true);
        $daysleft = round( ($date-time()) / 24 / 60 / 60);
    
       if($daysleft >= 0) {
    ?>
    <div class="coupon-date">
        <?php
        if($date == '')_e('Х?чинтэй', 'Couponize');
        else if($daysleft <= 0) _e('Дууссан', 'Couponize');
        else echo sprintf( _n('%d day left.', '%d Хоног ?лдсэн.', $daysleft, 'Couponize'), $daysleft );
    	?>
    </div>
    <?php
        }
         endwhile; ?>

    Thanks

    Thread Starter anandmongol

    (@anandmongol)

    Hi,

    Thanx for the suggestion, and it does work! :), but when I go to “next page” it is showing empty page, it seems generating empty placeholder when the $daysleft >= 0 criteria is not met. Any suggestion.

    My idea is to show only custom posts which are not expired. not not displaying the expired posts at all, on the homepage area.

    Thanx in advance
    ab

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Displaying custom posts only if custom meta box date is not expired’ is closed to new replies.