WP组职位(按年(Desc)>月(Desc)>日期(Asc))

时间:2015-09-03 作者:RooWM

我有一个自定义类别模板,可以根据自定义字段“event\\u date”对帖子进行分组。有两件事我无法正确理解:按年份和月份分组,按降序排列,而帖子本身则按升序排列。目前,这将为每个帖子创建“事件年”div和“事件月”。我想我在每个“年”和“月”都需要一些for/each语句但是,我只是没有经验把它写对。

因此,最终的标记是:

<div event-year>
    <div event-month>
        <article>
        <article>
    </div>
    <div event-month>
        <article>
    </div>
    <div event-month>
</div>
<div event-year>
    <div event-month>
        <article>
        <article>
    </div>
</div>
这就是我现在拥有的:

<?
$the_query = new WP_Query( array(
            \'post_type\'   => \'post\',
            \'post_status\' => \'publish\',
            \'meta_key\'    => \'event_date\',
            \'orderby\'     => \'meta_value\'
        ) );

        $current_header = \'\';
        $current_year = \'\';
        $current_month = \'\';
        $current_day = \'\';

        # The Loop
        while ( $the_query->have_posts() ) :
            $the_query->the_post();

            # get the event date
            $temp_date = get_post_meta( get_the_ID(), \'event_date\', true );

            Break up the date format
            $dateTime = DateTime::createFromFormat("Ymd", $temp_date);

            if ( is_object($dateTime) ) {
              $month = $dateTime->format(\'F\');
              $year = $dateTime->format(\'Y\');
              $day = $dateTime->format(\'d\');
              //...
            }

            $display_month = substr($month, 0, 3); 

            if($year != $current_year) {
                $current_year = $year;
                echo "<div id=\'event-year\' data-year-group=\'$year\'>";
            }

            if($month != $current_month) {
                $current_month = $month;
                echo "<div class=\'event-month\'><h2>$year - $month</h2>";
            }
?>
    <article> post stuff in here... </article>
<? endwhile; ?>

1 个回复
SO网友:Poonam
$last_month = null;
$args = array(
    \'post_type\' => \'post\',
    \'orderby\' => \'meta_value\',
    \'order\' => \'DESC\',
    \'post_status\' => \'publish\'
);

$the_query = new WP_Query($args);

while ( $the_query->have_posts() ) :
    $the_query->the_post();
    $the_month = get_the_time( \'Ym\' ); // e.g. 201611

    if ( $last_month !== $the_month ) {
        if ( $last_month !== null ) {
            // Close previously opened <div class="row" />
            echo \'</div>\';
        }

        echo \'<br/><div class="row \'.get_the_time("Y").\'" >\';
        echo \'MOnth: \'.get_the_time(\'m\');

    }

    $last_month = $the_month;

    ?>

    <div class="col-3">
        <?php the_title(); ?>
    </div>

    <?php

    if ( $the_query->current_post + 1 === $the_query->post_count ) {
        // Last item, always close the div
        echo \'</div>\';
    }

endwhile;

相关推荐

WordPress使用AJAX的UPDATE_USER_META onClick按钮

我有一个按钮,限制为7次单击,当单击数字5被禁用时,我的问题是,如果用户从不同的设备登录,计数将从0开始,与刷新页面相同。我的需要是,在user\\u meta中保存单击编号结果,如果再次单击,则更新,直到单击编号5,按钮更改为禁用。在DB user\\u meta info中:user\\u id:user\\u idmeta\\u键:clickCounterTravmeta\\u值:1(单击次数)这是页面加载时的代码,如果单击次数大于,则禁用按钮;4.Do Not Work:<script>