我不确定是否有一个特定的函数,因为我没有通过快速搜索找到任何东西,但我继续写了一个函数来完成这项工作,可以随意扩展,也可以用它做任何你喜欢的事情。
“按原样”提供代码类型的已发布帖子的列表年存档,您可以随意使用它。
function get_years_for_type( $type = \'post\', $echo = false, $sep = \'<br />\' ) {
global $wpdb, $wp_post_types;
$type = \'\' == $type ? \'post\' : $type;
if( !isset( $wp_post_types[$type] ) )
return;
$q = $wpdb->get_col( $wpdb->prepare( "SELECT YEAR(post_date) as year FROM wp_posts WHERE post_type = %s AND post_status = \'publish\' GROUP by year", $type ) );
if( empty( $q ) )
return;
$y = array();
foreach( $q as $year )
$y[] = \'<a href="\' . get_year_link( $year ) . \'">\' . $year . \'</a>\';
$y = implode( $sep, $y );
if( $echo )
echo $y;
else
return $y;
}
Example usage: // Output list of years for pages
echo get_years_for_type( \'page\' );
get_years_for_type( \'page\', true );
// Output list of years for posts
echo get_years_for_type();
get_years_for_type( \'post\', true );
// Storing the result in a variable
$var = get_years_for_type(); // post is assumed for type when none set
// Using an alternate seperator for the links
echo get_years_for_type( \'post\', false, \' | \' );
get_years_for_type( \'post\', true, \' | \' );
希望这有帮助。。