我有一个自定义帖子存档, 我想按年份将每个自定义帖子分组。这是我的模板的示例视图。我已经有了一个主意, 但我想使用高效的mos创建模板的方法。
这是我的模板视图
-----------------
| 2016 |
| Post Here |
| Post Here |
| Post Here |
-----------------
| 2015 |
| Post Here |
| Post Here |
| Post Here |
-----------------
| 2014 |
| Post Here |
| Post Here |
| Post Here |
我想让它尽可能地动态。任何建议都将受到高度赞赏。
#1
你可以尝试这样的事情。请享用!
// get years that have posts
$years = $wpdb->get_results( "SELECT YEAR(post_date) AS year FROM wp_posts WHERE post_type = 'encontros' AND post_status = 'publish' GROUP BY year DESC" );
foreach ( $years as $year ) {
// get posts for each year
$posts_this_year = $wpdb->get_results( "SELECT post_title FROM wp_posts WHERE post_type = 'encontros' AND post_status = 'publish' AND YEAR(post_date) = '" . $year->year . "'" );
//get permalinks for each post
$post_link = $wpdb->get_results( "SELECT get_the_permalink() FROM wp_posts WHERE post_type = 'encontros' AND post_status = 'publish' ");
foreach ( $posts_this_year as $post ) {
echo '<ul>' . $post->post_title . '</ul>';
}
echo '<h2><a href ="'.$posts_this_year.'">' . $year->year . '</a></h2><ul>';
echo '</ul>';
}
#2
我不喜欢推荐插件, 但是此插件完全可以满足你的需求, 这是最简单的方法。
好的, 顺便说一句, 你应该下载并安装Simple Yearly Archive插件。我从插件页面引用
你也可以从官方网站https://www.schloebe.de/wordpress/simple-yearly-archive-plugin/找到该插件的简码。
评论前必须登录!
注册