如何按年Wordpress分组存档

时间:2017-12-05 12:30:29

标签: php wordpress wordpress-theming custom-wordpress-pages

我有一个自定义的帖子档案我想按年份对每个自定义帖子进行分组。 这是我的模板的示例视图。我已经有了一个想法,但我希望以高效的方式创建我的模板。

继承我的模板视图

-----------------
|      2016     |
|    Post Here  |
|    Post Here  |
|    Post Here  |
-----------------
|      2015     |
|    Post Here  |
|    Post Here  |
|    Post Here  |
-----------------
|      2014     |
|    Post Here  |
|    Post Here  |
|    Post Here  |

我希望尽可能让它变得充满活力。 任何建议都受到高度赞赏。

2 个答案:

答案 0 :(得分: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>';
     }

答案 1 :(得分:0)

我不喜欢推荐插件,但这个插件完全符合您的需求,而且这是最简单的方法。

好的,您应该下载并安装Simple Yearly Archive插件。我引用了插件页面

你也可以从官方网站找到这个插件的短代码 https://www.schloebe.de/wordpress/simple-yearly-archive-plugin/