我已经使用元框为过去和即将发生的事件创建了代码。我只想显示最近的最近即将发布的帖子现在显示所有即将发布的帖子

时间:2018-10-10 09:41:15

标签: php wordpress meta meta-boxes

我已经使用meta框创建了。我只想在发布日期关闭时显示最近的最新帖子,而另一个最近的帖子需要显示该地方。我的代码显示所有即将发布的帖子。

function timer2_shortcode( $atts , $content = null ) {

    $atts = shortcode_atts(
        array(
            'category' => '',
        ),
        $atts
     );

     $args = array( 
       'post_type' => 'post',
       'category_name' =>  $atts['category'],
       'posts_per_page' => -1,       
       'meta_key' => 'event_start_date', 
       'orderby'           => 'meta_value',
       'order'             => 'DSC' 
     );

     $the_query = new WP_Query( $args );
     if ( $the_query->have_posts() ) {
         while ( $the_query->have_posts() ) {
             $the_query->the_post(); 

             if (has_post_thumbnail( $post->ID ) ) {

             $url = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );

             } 
             else {

                 $image = get_bloginfo('template_directory') . '/assets/images/default.png';

             }

             $start_date =  get_post_meta(get_the_ID(), 'event_start_date', true);

             $end_date =  get_post_meta(get_the_ID(), 'event_end_date', true);

             $start_time = strtotime($start_date);

             $start = date('Y-m-d',$start_time); 

             $end_time = strtotime($end_date);

             $end = date('Y-m-d',$end_time);

             $start_day = date('d',$start_time);

             $end_day = date('d',$end_time);

             $start_year = date('Y',$start_time);

             $end_year = date('Y',$end_time);

             $start_month = date('M',$start_time);

             $end_month = date('M',$end_time);

             if ($start == $end) {

                 $duration = $start_month. ' ' . $start_day.', '.$start_year;

             } else {

                 $duration = $start_month. ' ' . $start_day.' - '  . $end_month.' '.$end_day.', '.$start_year;

             }

             $current_date = date('Y-m-d');

             if ($start == $end) {

                 $duration = $start_month. ' ' . $start_day.', '.$start_year;

             } else {

                 $duration = $start_month. ' ' . $start_day.' - '  . $end_month.' '.$end_day.', '.$start_year;

             }

             $current_date = date('Y-m-d');

             if (($start  < $current_date && $end < $current_date) || (!$start_date)  || (!$end_date) ) {

                 if (has_post_thumbnail( $post->ID ) ) {

                     $url = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );

                     $imagesopen='<div id="custom-bg" style="background-image: url('. $url[0] .')">';
                     $imagesclose='</div>';

                } else {

                     $image = get_bloginfo('template_directory') . '/assets/images/default.png';

                     $images='<div id="custom-bg" style="background-image:url('.$image.')"></div>';
                }

                $outercat.='<div class="col-md-6 pad0">';

                $outercat.='<a href='.get_the_permalink().'>';

                $outercat.=$imagesopen;

                $outercat.='<div class="col-table ">';

                $outercat.='<div class="col-vcont">';

                $outercat.='<h1 class="title-link">'.get_the_title().'</h1>';

                $outercat.='</div>';

                $outercat.='</div>';

                $outercat.=$imagesclose;

                $outercat.='</a>';

                $outercat.='</div>';


            }

            if ( $end >= $current_date ) {


                $headcat.='<script>

                var countDownDate = new Date("'.countdown__get_meta( 'countdown__countdown_month' ) .countdown__get_meta( 'countdown__countdown_date' ).', '.countdown__get_meta( 'countdown__countdown_year' ).' 15:37:25").getTime();

                </script>'; 

                $headcat.='<div class="col-md-6 pad0">';

                $headcat.='<div class="first-blog-bg">';

                $headcat.='<div class="unique-bg" style="background-image: url('. $url[0] .')">';

                $headcat.='<div class="col-table ">';

                $headcat.='<div class="col-vcont">';

                $headcat.='<h1>'.get_the_title().'</h1>';

                $headcat .= '<p>' . limit_words(get_the_excerpt(), '20') . '</p>';  

                $headcat.='<span>'.activity_meta_get_meta( 'activity_meta_location' ).'</span>';

                $headcat.='<span>'.activity_meta_get_meta( 'activity_meta_time_' ).'</span>';

                $headcat.='<p class="testy" id="demo'.get_the_ID().'"></p>';

                $headcat.='</div>';

                $headcat.='</div>';

                $headcat.='</div>';

                $headcat.='</div>';

                $headcat.='</div>';

           }    

           wp_reset_postdata();

       }

    }

    $out.='<section class="section category-parent">';

    $out.='<div class="container">';

    $out.='<div class="row">';

    $out.='<div class="col-md-6">';

    $out.='<div class="row">';

    $out.=$outercat;

    $out.='</div>';

    $out.='</div>';

    $out.=$headcat;

    $out.='</div>';

    $out.='</section>';

    return $out;

}

add_shortcode( 'timer2', 'timer2_shortcode' );

1 个答案:

答案 0 :(得分:0)

通过设置'posts_per_page' => -1,,您将获得所有帖子。如果您只想显示5个帖子(事件),请将其设置为5。'posts_per_page' => 5,

然后更改您的$args以包括WHERE子句,以选择event_start_date大于今天的日期的帖子。可以在这里找到一种方法:https://wordpress.stackexchange.com/questions/57979/wp-query-show-posts-that-end-later-than-today