admin管理员组文章数量:1122846
Need group posts from custom post type by post date.
Example:
Jan (all posts posted in januar for all years 2010,2011 ... 2015)
1 (all posts posted in 1 januar for all years 2010,2011 ... 2015)
POST1
POST2
...
2 (all posts posted in 1 januar for all years 2010,2011 ... 2015)
POST1
POST2
...
Feb (all posts posted in januar for all years 2010,2011 ... 2015)
1 (all posts posted in 1 januar for all years 2010,2011 ... 2015)
POST1
POST2
...
Now I use this code:
<table class="table table-striped">
<?php
$current_month = '';
$args = array(
'post_type' => 'dayinhistory',
'date_query' => array(
'month' => '1',
),
'order' => 'ASC'
);
query_posts( $args );
if ( have_posts() ) : while( have_posts() ): the_post();
$this_month = get_the_time( 'F' );
if( $this_month != $current_month ){
$current_month = $this_month;
echo '<tr><td colspan="4">';
echo $current_month;
echo '</td></tr>';
}
?>
<tr>
<td style="vertical-align:inherit;"><?php the_date('d.m.Y', '<span class="label label-info">', '</span>'); ?></td>
<td style="vertical-align:inherit;"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></td>
<td style="vertical-align:inherit;"><span class="label label-default"><?php the_time('d M Y'); ?></span></td>
</tr>
<?php endwhile;?>
<?php endif;?>
<?php wp_reset_query();?>
</table>
Or this:
<?php
$the_query = new WP_Query( 'post_type=dayinhistory&monthnum=3' );
echo '<table class="table table-striped">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$this_month = get_the_time( 'F' );
if( $this_month != $current_month ){
$current_month = $this_month;
echo '<tr><td colspan="4">'; echo $current_month; echo '</td></tr>';
}
?>
<tr>
<td style="vertical-align:inherit;"><?php the_date('d.m.Y', '<span class="label label-info">', '</span>'); ?></td>
<td style="vertical-align:inherit;"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></td>
<td style="vertical-align:inherit;"><span class="label label-default"><?php the_time('d M Y'); ?></span></td>
</tr>
<?php }
echo '</table>';
wp_reset_postdata();
?>
Result
But posts sorts by year, not by day.
Need group posts from custom post type by post date.
Example:
Jan (all posts posted in januar for all years 2010,2011 ... 2015)
1 (all posts posted in 1 januar for all years 2010,2011 ... 2015)
POST1
POST2
...
2 (all posts posted in 1 januar for all years 2010,2011 ... 2015)
POST1
POST2
...
Feb (all posts posted in januar for all years 2010,2011 ... 2015)
1 (all posts posted in 1 januar for all years 2010,2011 ... 2015)
POST1
POST2
...
Now I use this code:
<table class="table table-striped">
<?php
$current_month = '';
$args = array(
'post_type' => 'dayinhistory',
'date_query' => array(
'month' => '1',
),
'order' => 'ASC'
);
query_posts( $args );
if ( have_posts() ) : while( have_posts() ): the_post();
$this_month = get_the_time( 'F' );
if( $this_month != $current_month ){
$current_month = $this_month;
echo '<tr><td colspan="4">';
echo $current_month;
echo '</td></tr>';
}
?>
<tr>
<td style="vertical-align:inherit;"><?php the_date('d.m.Y', '<span class="label label-info">', '</span>'); ?></td>
<td style="vertical-align:inherit;"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></td>
<td style="vertical-align:inherit;"><span class="label label-default"><?php the_time('d M Y'); ?></span></td>
</tr>
<?php endwhile;?>
<?php endif;?>
<?php wp_reset_query();?>
</table>
Or this:
<?php
$the_query = new WP_Query( 'post_type=dayinhistory&monthnum=3' );
echo '<table class="table table-striped">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$this_month = get_the_time( 'F' );
if( $this_month != $current_month ){
$current_month = $this_month;
echo '<tr><td colspan="4">'; echo $current_month; echo '</td></tr>';
}
?>
<tr>
<td style="vertical-align:inherit;"><?php the_date('d.m.Y', '<span class="label label-info">', '</span>'); ?></td>
<td style="vertical-align:inherit;"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></td>
<td style="vertical-align:inherit;"><span class="label label-default"><?php the_time('d M Y'); ?></span></td>
</tr>
<?php }
echo '</table>';
wp_reset_postdata();
?>
Result
But posts sorts by year, not by day.
Share Improve this question edited Mar 3, 2015 at 20:01 KingStakh asked Mar 3, 2015 at 12:45 KingStakhKingStakh 1136 bronze badges 1- 1 What have you tried so far and what did not work. Please file an edit with your efforts in order for someone to help you :-) – Pieter Goosen Commented Mar 3, 2015 at 13:01
1 Answer
Reset to default 0You can use the month
parameter of WP_Query to get posts by month. For example, to get all posts published on March, ignoring year:
<?php
$the_query = new WP_Query( 'monthnum=3' );
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
wp_reset_postdata();
?>
本文标签: group posts by monthdate ignore years
版权声明:本文标题:group posts by monthdate ignore years 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736280919a1926153.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论