我试图在两个特定的日历月之间获得wp帖子。问题是,这是一年。所以这个月从五月开始,到明年四月结束。因此,每年的产量应该从年初的5月开始,到明年的4月。(例如:2015年5月至2016年4月)
我在一些插件的帮助下尝试了一下,但它只会在一个日历年(1-12月)左右输出帖子。
完成php后,我需要显示2015年5月至2016年4月的博客列表,从下拉列表中选择。。。
我的代码如下;
$args=array(
\'orderby\' => \'date\',
\'order\' => \'ASC\',
\'posts_per_page\' => 1,
\'caller_get_posts\'=>1
);
$oldestpost = get_posts($args);
$args=array(
\'orderby\' => \'date\',
\'order\' => \'DESC\',
\'posts_per_page\' => 1,
\'caller_get_posts\'=>1
);
$newestpost = get_posts($args);
if ( !empty($oldestpost) && !empty($newestpost) ) {
$oldest = mysql2date("Y", $oldestpost[0]->post_date);
$newest = mysql2date("Y", $newestpost[0]->post_date);
$years = $newest - $oldest;
echo \'<select onchange="if (this.value) window.location.href=this.value"><option value="" selected>Select Year</option>\';
for ( $counter = 0; $counter <= $years; $counter += 1) {
$endDate = $oldest+1;
$where = apply_filters( \'getarchives_where\', "WHERE post_type = \'post\' AND post_status = \'publish\' AND post_date BETWEEN \'$oldest-05-01\' AND \'$endDate-04-30\'" );
$join = apply_filters( \'getarchives_join\', \'\' );
if ( $months = $wpdb->get_results( "SELECT YEAR(post_date) AS year, MONTH(post_date) AS numMonth, DATE_FORMAT(post_date, \'%M\') AS month, count(ID) as post_count FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC" ) ) {
foreach ( $months as $month ) {
$currentYear = $month->year;
if ( $prevYear !== $currentYear ) {
echo \'<option value=\' . esc_url( get_year_link( $month->year ) ) .\'>\'. esc_html( ($currentYear).\'-\'.($currentYear+1) ) . \'</option>\';
}
$prevYear = $currentYear;
if ( ( $currentYear !== $prevYear ) && ( \'\' !== $prevYear ) ) {
echo \'</select>\';
}
}
}
$oldest++;
}
?>
<?php
echo \'</select>\';
}
**Elaborating this question with a list---Just an illustrative.**
*if 2006 selected the archive page will list post like below.
o May 2006
o June 2006
o July 2006
o August 2006
o September 2006
o October 2006
o November 2006
o December 2006
o January 2007
o February 2007
o March 2007
o April 2007
Like above all other year lists same...
* 2005
* 2004
* 2003
* 2002