Ich habe einen kurzen Code (Teil eines Plugins) erstellt, um die Posts anzuzeigen, die nicht abgelaufen sind. Zum Speichern des Ablaufdatums habe ich bereits einen Metaschlüssel mit dem Namen newsbox-date erstellt.
Praktisch für Micro-News, Kurznachrichten und Urlaubsanzeigen. Mein Kunde wird die Beiträge nicht zum Bloggen verwenden, sondern mehrmals für kurze Nachrichten. Mit dem Shortcode können wir die Newsbox auf der Homepage und Kontaktseite einfügen. (wie erwähnt: praktisch für den Urlaub)
Bisher habe ich folgendes:
function my_recent_posts_shortcode( $atts ) { extract( shortcode_atts( array( 'limit' => 5 ), $atts ) ); $q = new WP_Query( 'posts_per_page=' . $limit ); $list = ''; 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 .= '' . get_the_title() . '
' . get_the_content() . ''; } } wp_reset_query(); return $list . ''; } add_shortcode( 'recent-posts', 'my_recent_posts_shortcode' );
Es gibt ein paar Dinge, die ich handhaben muss:
Danke für irgendwelche Tipps. Wenn dieses Ding funktioniert, werde ich das Plugin im Rep teilen.
Danke, ich habe es jetzt; in der Tat mit Meta-Abfrage.
function my_recent_posts_shortcode( $atts ) { global $post; $currenttime = current_time("mysql"); $args = array( 'post_type' => 'post', 'meta_query' => array( array( 'key' => 'newsbox-date', 'value' => $currenttime, 'compare' => '>' ) ) ); $newsbox = new WP_Query( $args ); if ( $newsbox->have_posts() ) : $list = ''; while ( $newsbox->have_posts() ) : $newsbox->the_post(); $list .= '' . get_the_title() . '
' . get_the_content() . ''; endwhile; $list .= ''; endif; wp_reset_query(); return $list; } add_shortcode( 'recent-posts', 'my_recent_posts_shortcode' );