• I’ve made a shortcode to display the posts which aren’t expired. Handy for micro-news, short messages and holliday announcements.
    For saving the expiration date, i made a meta key newsboy-date.

    So far i have this:

    function my_recent_posts_shortcode( $atts ) {
    	extract( shortcode_atts( array( 'limit' => 5 ), $atts ) );
    	$q = new WP_Query( 'posts_per_page=' . $limit );
    	$list = '<div class="newsbox-posts">';
    	while ( $q->have_posts() ) {
    		$q->the_post();
    		$expdate = get_post_meta(get_the_ID(), 'newsbox-date', true);
    		$nowdatetime = current_time("mysql");
    		if ($expdate >= $nowdatetime) {
    		$list .= '<h4>' . get_the_title() . '</h4>' . get_the_content() . '';
    		}
    	}
    	wp_reset_query();
    	return $list . '</div>';
    }
    add_shortcode( 'recent-posts', 'my_recent_posts_shortcode' );

    There are a few things i’m missing
    – can i do the filtering of expiration date in the WP_Query already ? Now all posts are first requested.
    – When all posts are expired, the div newsboy-posts must not be showed.

    Thanks for any help.

  • The topic ‘Micro newsblock shortcode’ is closed to new replies.