我如何按月和年对帖子进行分组?

时间:2019-08-11 作者:MAR

我有一个自定义帖子类型下的帖子,我喜欢在按年份和月份分组的页面中显示。在我创建的页面模板中,我设法按月份将它们分开,但仍然没有在不同的级别进行分组(它们都是相同的级别,只有月份名称作为组之间的分隔符),like so:

<div class="month"></div>
<div>post title</div>
<div>post title</div>
<div>post title</div>
<div>post title</div>
<div class="month"></div>
<div>post title</div>
<div>post title</div>
<div>post title</div>
<div>post title</div>
<div class="month"></div>
<div>post title</div>
<div>post title</div>
<div>post title</div>
<div>post title</div>

I did that with:

<?php
global $more; $more = false; # some wordpress wtf logic
$posts = get_posts(array(
    \'post_type\' => \'press\',
    \'nopaging\' => true,
    \'orderby\' => \'date\',
    \'order\' => \'DSC\',
    \'posts_per_page\'   => 999999,
    ));

$month = null;

foreach($posts as $post): 

    setup_postdata($post); //enables the_title() etc. without specifying a post ID
    $postMonth =  date(\'FY\',strtotime($post->post_date));

    if($month!=$postMonth){
        echo \'<div class="month">\'.date(\'F Y\',strtotime($post->post_date)).\'</div>\';        
        $month =$postMonth;
    } ?>

    <div><a href="<?php the_field(\'url\');?>">"<?php the_title();?>"</a></div>

<?php endforeach;?>

What I want to have is something like this:

<div class="year">
    <span>2019</span>
    <div class="month">
        <span>August</span>
            <div>post title</div>
            <div>post title</div>
    </div>
</div>
<div class="year">
    <span>2019</span>
    <div class="month">
        <span>July</span>
            <div>post title</div>
            <div>post title</div>
    </div>
</div>
<div class="year">
    <span>2018</span>
    <div class="month">
        <span>January</span>
            <div>post title</div>
            <div>post title</div>
    </div>
</div>
我尝试了StackExchange上的所有解决方案,但都没有成功。有什么想法吗?

1 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

您可以这样做:

global $post;

$posts = get_posts( array(
    \'post_type\' => \'press\',
    \'nopaging\'  => true,
    \'orderby\'   => \'date\',
    \'order\'     => \'DESC\', // it\'s DESC; not DSC
    // There\'s no use setting posts_per_page when nopaging is enabled.
    // Because posts_per_page will be ignored when nopaging is enabled.
) );

$_year_mon = \'\';   // previous year-month value
$_has_grp = false; // TRUE if a group was opened
foreach ( $posts as $post ) {
    setup_postdata( $post );

    $time = strtotime( $post->post_date );
    $year = date( \'Y\', $time );
    $mon = date( \'F\', $time );
    $year_mon = "$year-$mon";

    // Open a new group.
    if ( $year_mon !== $_year_mon ) {
        // Close previous group, if any.
        if ( $_has_grp ) {
            echo \'</div><!-- .month -->\';
            echo \'</div><!-- .year -->\';
        }
        $_has_grp = true;

        echo \'<div class="year">\';
        echo "<span>$year</span>";

        echo \'<div class="month">\';
        echo "<span>$mon</span>";
    }

    // Display post title.
    if ( $title = get_the_title() ) {
        echo "<div>$title</div>";
    } else {
        echo "<div>#{$post->ID}</div>";
    }

    $_year_mon = $year_mon;
}

// Close the last group, if any.
if ( $_has_grp ) {
    echo \'</div><!-- .month -->\';
    echo \'</div><!-- .year -->\';
}

wp_reset_postdata();

相关推荐

Vagrant Access index.php

我相信这是一个合适的地方。首先,我安装了VirtualBox和Vagrant。在那之后,我将cd刻录到以下内容:https://github.com/LearnWebCode/vagrant-lamp (下载了zip,提取了文件夹)然后在运行vagrant up之后,我到达了Congratulations 通过IP分页http://192.168.56.101/之后,我在hosts文件中设置了一个易于阅读的URL,并可以运行它,但它会导致出现祝贺页面,而不是索引。php的Hello World。你知道怎么