如何按年份将存档帖子分组

我有一个自定义帖子存档, 我想按年份将每个自定义帖子分组。这是我的模板的示例视图。我已经有了一个主意, 但我想使用高效的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/找到该插件的简码。

来源:

https://www.srcmini02.com/63361.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_37643.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?