按月显示帖子(带有月份标题)- Wordpress
Display posts by month (with month title) - Wordpress
我想实现这样的目标,我不知道是否可行,最好的方法是什么:
http://oi60.tinypic.com/21dokqu.jpg
我查询帖子的方式是这样的:
<div class="post">
<?php global $wp_query;
query_posts( array('post_type' => array( 'lecturas-post' ),'showposts' => 15, 'paged'=>$paged, 'order' => 'DESC' ));?>
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<div><?php the_title() ?></a>
<?php endwhile; // end of the loop. ?>
</div>
任何人都可以告诉我怎么做或最好的方法吗?
这是我试过的,但没有显示任何内容:
global $wpdb; // Don't forget
$collection = $wpdb->get_results("
SELECT YEAR(p.post_date) AS post_year, MONTH(p.post_date) AS post_month, p.*
FROM {$wpdb->posts} AS p
WHERE p.post_type = 'post' AND p.post_status = 'publish'
ORDER BY p.post_date DESC
", OBJECT );
// Loop once to grab the years
foreach ( $collection as $year ):
// Loop for a second time to grab the months inside a year
foreach ( $collection as $month ):
// Continue unless years match
if ( $month->post_year != $year ) continue;
// Loop one more time to grab the posts inside the month, inside the year
foreach ( $collection as $post ):
// Continue unless months and years match
if ( $post->post_year != $year || $post->post_month != $month ) continue;
// You can use the posts data here
endforeach;
endforeach;
endforeach;
试试这个,希望对你有用。
$args = array('posts_per_page' => -1, 'orderby' => 'date' );
$myQuery = new WP_Query($args);
$date = '';
if ( $myQuery->have_posts() ) : while ( $myQuery->have_posts() ) : $myQuery->the_post();
if ( $date != get_the_date() ) {
echo $date;
echo '<hr />';
$date = get_the_date();
}
the_title(); // or whatever you want here.
echo '<br />';
endwhile; endif;
wp_reset_postdata();
此处查询的更多信息:http://codex.wordpress.org/Class_Reference/WP_Query
我从这里找到答案。
Group posts by date in Wordpress
给出的答案
希望这会有所帮助:
$args = array(
'post_type' => 'lecturas-post',
'numberposts' => 15,
'orderby' => 'post_date',
'order' => 'DESC',
);
$posts = wp_get_recent_posts( $args );
$collection = array();
foreach ( $posts as $lectura ){
$index = date( 'Y-m', strtotime($lectura['post_date']) );
$collection[ $index ][] = $lectura;
}
$collection 现在包含您每月的帖子,您可以轻松地循环浏览它们。
我想实现这样的目标,我不知道是否可行,最好的方法是什么:
http://oi60.tinypic.com/21dokqu.jpg
我查询帖子的方式是这样的:
<div class="post">
<?php global $wp_query;
query_posts( array('post_type' => array( 'lecturas-post' ),'showposts' => 15, 'paged'=>$paged, 'order' => 'DESC' ));?>
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<div><?php the_title() ?></a>
<?php endwhile; // end of the loop. ?>
</div>
任何人都可以告诉我怎么做或最好的方法吗?
这是我试过的,但没有显示任何内容:
global $wpdb; // Don't forget
$collection = $wpdb->get_results("
SELECT YEAR(p.post_date) AS post_year, MONTH(p.post_date) AS post_month, p.*
FROM {$wpdb->posts} AS p
WHERE p.post_type = 'post' AND p.post_status = 'publish'
ORDER BY p.post_date DESC
", OBJECT );
// Loop once to grab the years
foreach ( $collection as $year ):
// Loop for a second time to grab the months inside a year
foreach ( $collection as $month ):
// Continue unless years match
if ( $month->post_year != $year ) continue;
// Loop one more time to grab the posts inside the month, inside the year
foreach ( $collection as $post ):
// Continue unless months and years match
if ( $post->post_year != $year || $post->post_month != $month ) continue;
// You can use the posts data here
endforeach;
endforeach;
endforeach;
试试这个,希望对你有用。
$args = array('posts_per_page' => -1, 'orderby' => 'date' );
$myQuery = new WP_Query($args);
$date = '';
if ( $myQuery->have_posts() ) : while ( $myQuery->have_posts() ) : $myQuery->the_post();
if ( $date != get_the_date() ) {
echo $date;
echo '<hr />';
$date = get_the_date();
}
the_title(); // or whatever you want here.
echo '<br />';
endwhile; endif;
wp_reset_postdata();
此处查询的更多信息:http://codex.wordpress.org/Class_Reference/WP_Query
我从这里找到答案。 Group posts by date in Wordpress
给出的答案希望这会有所帮助:
$args = array(
'post_type' => 'lecturas-post',
'numberposts' => 15,
'orderby' => 'post_date',
'order' => 'DESC',
);
$posts = wp_get_recent_posts( $args );
$collection = array();
foreach ( $posts as $lectura ){
$index = date( 'Y-m', strtotime($lectura['post_date']) );
$collection[ $index ][] = $lectura;
}
$collection 现在包含您每月的帖子,您可以轻松地循环浏览它们。