简体   繁体   English

如何根据日期对WordPress中的帖子进行分组?

[英]How to group ans sort posts in wordpress according to date?

I have the following code that sorts the posts by title. 我有以下代码按标题对帖子进行排序。 See attached screenshot. 请参阅所附的屏幕截图。

desired result 理想的结果 这就是我要的

This is what i am getting right now 这就是我现在得到的

这就是我现在得到的

What I want is group the posts together ie posts with Date as December should come under December and those with November should be under november as so on. 我想要的是将帖子归为一组,即日期为12月的帖子应归入12月,而日期为11月的帖子应归纳为11月。 Both images attached. 附有两个图像。

I need some tweaks in the code which is below. 我需要在下面的代码中进行一些调整。

                 <div class="margin-top" style="margin-top: 20px;">
                <?php if ( have_posts() ) : ?>

                    <div class="articles-list">

                        <?php do_action( 'presscore_before_loop' ); ?>

                        <?php update_post_thumbnail_cache(); ?>

                                <?php while ( have_posts() ) : the_post(); ?>

                                <?php 

  $start=0;
  $venue = basename(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));

  if (isset($venue) && !empty($venue)) {
      global $post;
      $venue_a = get_term_by('slug',$venue, 'venues');
      for($i=0;$i<10;$i++){
        $key = get_post_meta($post->ID,'sub_seminars_'.$i.'_venue',true);

        if($venue_a->name == $key ){
          $ref = get_post_meta($post->ID,'sub_seminars_'.$i.'_ref',true); 
          $ven_label = get_post_meta($post->ID,'sub_seminars_'.$i.'_venue',true);   
          $start_date = get_post_meta($post->ID,'sub_seminars_'.$i.'_start_date',true);   
          $end_date = get_post_meta($post->ID,'sub_seminars_'.$i.'_end_date',true);   
          $fees = get_post_meta($post->ID,'sub_seminars_'.$i.'_fees',true); 
          $start =1;
          break;
        }
      }
  }


    ?>

                <table class="gry-bkgrnd-sec" style=" border-collapse: collapse; width: 100%;">
                     <tbody>
                          <?php if( have_rows('sub_seminars') ): ?>
                          <?php while ( have_rows('sub_seminars') ) : the_row();

                            ?>
                                <tr style="background-color: #50619a">
                                    <td style="width: 100px; color: #fff;">Reference</td>
                                    <td style="width: 400px; color: #fff;">Title</td>
                                    <td style="width: 200px; color: #fff;">Dates</td>
                                    <td style="width: 100px; color: #fff;">Venue</td>
                                     <td style="width: 100px; color: #fff;">Fees</td>
                                </tr>

                                <tr>
                              <td style="width: 200px;"><?php the_sub_field('ref'); ?></td>
                                <td style="width: 400px;">                 


                                   <a style=" color: #044970;" href="<?php the_permalink(); ?>" title="<?php echo the_title_attribute( 'echo=0' ); ?>" rel="bookmark">
                                       <?php the_title(); ?></a><?php if (get_field('seminar_sub_title')) : ?>
                                       <a style=" color: #044970;" href="<?php the_permalink(); ?>" rel="bookmark">
                                           <?php the_field('seminar_sub_title'); ?><?php endif; ?></a>

                              </td>

                              <td>


                             <?php
                             if(date("m", strtotime($start_date)) != date("m", strtotime($end_date)))
                             {
                             echo date("d M", strtotime($start_date)).' - '.date("d M", strtotime($end_date));
                             }
                             else
                             {
                             echo date("d", strtotime($start_date)).' - '.date("d M", strtotime($end_date));

                             }
                             ?>


                              </td>

                              <td style="width: 45px;">
                              <?php echo $ven_label; ?>
                              </td>

                               <td style="width: 45px;">
                                   <?php  echo $fees; ?>
                               </td>

                               </tr>


                               <?php break; ?>
                               <?php endwhile; ?>
                               <?php endif; ?>

                               </tbody>
                               </table>

                        <?php endwhile; ?>

                        <?php do_action( 'presscore_after_loop' ); ?>

                    </div>

                    <?php dt_paginator(); ?>

                <?php else : ?>

                    <?php get_template_part( 'no-results', 'blog' ); ?>

                <?php endif; ?>

     </div>

The venues shown is a custom taxonomy created 显示的场地是创建的自定义分类法

This is the code I wrote t fix the above problem 这是我写的代码无法解决上述问题

<?php
$archive_data = get_queried_object();
//echo '<pre>';print_r( $archive_data );
$archiveId = $archive_data->term_id;
$categroy_name =  $archive_data->slug;
$seminar_venue = $archive_data->name;
$subseminars_found = euromatech_search_subseminars( '', $archive_data->slug, '');
 if(!empty($subseminars_found)){
    foreach($subseminars_found as $month => $day){
        ?>
        <div class="dec-box">
            <h3 class="year-title"><?php echo date('M Y', $month); ?></h3>
            <div class="register-table table-sec">
                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                    <tr class="background-g">
                        <th class="left-td seminar-title-header">Program Title</th>
                        <th class="text-center"><?php echo date('Y', $month); ?> Dates</th>
                        <th class="text-center">Venues</th>
                    </tr>
                    <?php

                    foreach($day as $subseminars){
                        foreach($subseminars as $subseminar) {

                            ?>

                            <tr>
                                <td class="left-td"><a href="<?php the_permalink($subseminar['seminarID']); ?>">
                                        <?php echo get_the_title($subseminar['seminarID']); ?> <?php if (get_field('seminar_sub_title', $subseminar['seminarID'])) : ?><?php echo get_field('seminar_sub_title', $subseminar['seminarID']); ?><?php endif; ?></a></td>
                                <td class="date-year text-center"><?php echo date('d M', strtotime($subseminar['start_date'])) . ' - ' . date('d M', strtotime($subseminar['end_date'])); ?></td>
                                <td class="text-center"><?php echo esc_html($subseminar['venue_name']); ?></td>
                            </tr>

                            <?php                          
                        }
                    }
                    ?>
                </table>
            </div>
        </div>

        <?php
    }

} 

else {

    echo '<h4>No Upcoming Seminars found</h4>';
    echo '<p>For further information on forthcoming events in this region, please contact <a href="mailto:',ot_get_option('email_address', ''),'">',ot_get_option('email_address', ''),'</a></p>';
}

?>

Hope it Helps someone else. 希望它对别人有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM