如何按年份对存档 post 进行分组 Wordpress

How to group archive post by year Wordpress

我有一个自定义 post 存档我想按年份对每个自定义 post 进行分组。 这是我的模板示例视图。我已经有了一个想法,但我想要最有效的方法来创建我的模板。

这是我的模板视图

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

我想尽我所能让它充满活力。 非常感谢任何建议。

我不喜欢推荐插件,但这个插件正是您所需要的,而且是最简单的方法。

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

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


你可以尝试这样的事情。享受吧!


 // 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>';
     }