Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

i am building a News site. I want to display 1 featured post from "prime" category for only few hours. after it should be expired or disappeared from the section.

please have a look here u can see the post on top.

 <div id="prime_news" class="col-full">
 <?php $the_query = new WP_Query('category_name=prime&showposts=1&orderby=post_date&order=desc'); 
    if ($the_query->have_posts()) :?>
<div id="prime_headline">
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<?php endwhile; ?>
<div class="prime_left fl">
<h3><a href="<?php the_permalink(); ?>" title="" class="title_slide"><?php the_title(); ?></a></h3>
<p class="prime_excerpt"><?php echo limit_words(get_the_excerpt(), '190'); ?><span  class="read-more"><a href="<?php the_permalink(); ?>"><?php _e('&#2310;&#2327;&#2375;  &#2346;&#2338;&#2375;...', 'BharatNews'); ?></a></span></p></div>
<div class="prime_right fr"><?php bharti_get_image('image',560,320,' '.$GLOBALS['align']); ?></div>
</div><div class="clear"></div> 
    <?php endif; ?>

 </div> 
share|improve this question

2 Answers 2

up vote 3 down vote accepted

Check the time parameter on WP_Query Class. There is a example on how to show posts form last 30 days. You can do it for last couple of hours, minutes or even seconds! Code is tested and working.

$args = array(
                            'posts_per_page' => 1, // We are showing only one post
                            'category_name' => 'prime' // showing form category prime
                            );


add_filter( 'posts_where', 'filter_where' );
$the_query = new WP_Query( $args );
remove_filter( 'posts_where', 'filter_where' ); ?>

 <div id="prime_news" class="col-full">
 <?php
    if ($the_query->have_posts()) :?>
<div id="prime_headline">
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<?php endwhile; ?>
<div class="prime_left fl">
<h3><a href="<?php the_permalink(); ?>" title="" class="title_slide"><?php the_title(); ?></a></h3>
<p class="prime_excerpt"><?php echo limit_words(get_the_excerpt(), '190'); ?><span  class="read-more"><a href="<?php the_permalink(); ?>"><?php _e('&#2310;&#2327;&#2375;  &#2346;&#2338;&#2375;...', 'BharatNews'); ?></a></span></p></div>
<div class="prime_right fr"><?php bharti_get_image('image',560,320,' '.$GLOBALS['align']); ?></div>
</div><div class="clear"></div> 
    <?php endif; ?>

 </div> 

Add this function on functions.php

function filter_where( $where = '' ) {
    // posts published last 30 minutes
    $where .= " AND post_date > '" . date('Y-m-d H:i:s', strtotime('-30 minutes')) . "'";
    return $where;
}
share|improve this answer
    
i tried this.... but nothing happened. it showing blank .... (see: alturl.com/3p3k8 ) please have a look my code pastebin.com/2vTLPzQm –  Nisha_at_Behance Jul 23 '12 at 10:35
    
change strtotime('+ 1 hours')) to strtotime('-1 hours')) Note the ( - ) negative sign. –  Sisir Jul 23 '12 at 13:32
    
great its working ... but a small problem. i count excerpts '240' but its showing default '55' <?php echo limit_words(get_the_excerpt(), '240'); ?> –  Nisha_at_Behance Jul 24 '12 at 5:09
    
now i posted this 'prime_news' at 10:21 am still showing at 1:21 what it's means ' date('Y-m-d', strtotime('- 2 hours')) ' –  Nisha_at_Behance Jul 24 '12 at 7:52
    
@Nisha_Behance: check this page for excerpt length. And don't use space between negative sign and number. –  Sisir Jul 24 '12 at 16:24

You can achieve this by using a shortcode. Here is a function that adds a shortcode called expire that you can add a value to with date and time when you want the content to be hidden.

/**
 * Make a simple expire shortcode
 * Example: [expires hide="2012-07-18 20:00:00"]
*/

function pa_hideafter_shortcode( $args = array(), $content = '' ) {
    extract( shortcode_atts(
        array(
            'hide' => 'tomorrow',
        ),
        $args
    ));
    if ( strtotime( $hide ) > time() ) {
        return $content;
    }
    return '';
}

add_shortcode('expires', 'pa_hideafter_shortcode');

And put this shortcode in your editor: [expires hide="2012-07-18 20:00:00"] Your content here [/expires]

This content will be hidden today after 20:00.

Or you can use an if statement to check if its true or not by adding the current time and add +1 hour and check on either the_time or post_date Try this:

<div id="prime_news" class="col-full">
    <?php
    // The time right now + one hour
    $time_remove = date( 'Y-m-d H:i:s', strtotime ( '+1 hour' . date( 'Y-m-d H:i:s' ) ) );
    ?>

    <?php $the_query = new WP_Query('category_name=prime&showposts=1&orderby=post_date&order=desc'); ?>

    <?php if ( $the_query->have_posts() ) : ?>
        <div id="prime_headline">
            <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?><?php endwhile; ?>

            <?php if( $time_remove <= the_time('Y-m-d H:i:s') ) : ?>

                <div class="prime_left fl">
                    <h3><a href="<?php the_permalink(); ?>" title="" class="title_slide"><?php the_title(); ?></a></h3>
                    <p class="prime_excerpt">
                        <?php echo limit_words( get_the_excerpt(), '190'); ?>
                        <span  class="read-more">
                            <a href="<?php the_permalink(); ?>"><?php _e('&#2310;&#2327;&#2375;  &#2346;&#2338;&#2375;...', 'BharatNews'); ?></a>
                        </span>
                    </p>
                </div>

                <div class="prime_right fr">
                        <?php bharti_get_image('image',560,320,' '.$GLOBALS['align']); ?>
                </div>

            <?php endif; ?>

        </div>
    <?php endif; ?>

    <div class="clear"></div>

</div>

This is untested on my system but it is pretty straight forward. Make a variable with the current date + the time you want the post to be displayed i.e 1 hour ( Change it in $time_remove ) then get the posts publish time with the_time('Y-m-d H:i:s') and check if the $time_remove is Less than or equal to (<=) to the publish date the_time('Y-m-d H:i:s') then display the post.

share|improve this answer
    
thanks @Pontus but time still showing here alturl.com/fkt54 & if i want to display post only for few hours ? –  Nisha_at_Behance Jul 19 '12 at 4:16
    
it's not working, plz check site. section not showing –  Nisha_at_Behance Jul 19 '12 at 4:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.