将帖子状态更新作为前端新闻公告执行

时间:2016-04-17 作者:KDX

是否有方法实现在主页上打印状态更新的功能。

例如:。

2016/04/17, there are 2 articles posted in category ABC
2016/04/17, there are 3 articles posted in category DEF
2016/04/15, there are 5 articles posted in category XYZ
我想保持7天的滚动记录,作为新闻更新显示在主页或侧边栏上。

文章为自定义文章类型。

1 个回复
最合适的回答,由SO网友:dan9vu 整理而成

希望这能帮助:

function wpse_display_post_counts_by_categories() {

    $categories = get_categories();
    $container = [];

    foreach ($categories as $category) {
        $args = [
            \'post_type\'      => \'post\', // Change to your post type.
            \'post_status\'    => \'publish\',
            \'category\'       => $category->term_id,
            \'orderby\'        => \'post_date\',
            \'order\'          => \'DESC\',
            \'posts_per_page\' => -1
        ];
        $posts = get_posts($args);
        if ( count($posts) ) { // Indexing by post date of the latest post in the category.
            $container[strtotime($posts[0]->post_date)] = [
                \'id\'     => $category->term_id,
                \'name\'   => $category->name,
                \'counts\' => count($posts)
            ];
        }
    }

    krsort($container); // Sort by key(post date), from high to low to get latest updated categories.

    $values = array_slice($container, 0, 3, true); // Get only 3 latest updated categories.

    foreach ($values as $key => $value) {
        $output = sprintf( __(\'%s, there are %d articles posted in category <a href="%s">%s</a>\', \'text-domain\'), date_i18n(\'Y/m/d\', $key), $value[\'counts\'], esc_url( get_term_link($value[\'id\'], \'category\') ), $value[\'name\']);
        echo \'<p>\' . $output . \'</p>\'; // Whatever markup you need.
    }

}
IMHO:目前,我还没有找到一种更有效的方法。我不知道你怎么处理$categories 当有太多的东西无法循环时。我不建议这样做。仅仅为了打印出一些状态就太多了。

相关推荐