如何从主题访问类别信息?

时间:2020-04-01 作者:metichi

我正在制作一个wordpress网站来托管我的艺术作品和漫画,每个漫画系列都有自己的类别,所以我想通过我制作的主题/插件在网站中显示类别信息。

我想做三件事:

使用类别ID显示类别标题、描述和存档的url。获取该类别中最旧和最新帖子的ID,以便我可以添加指向“开始阅读”和“阅读最新帖子”的链接。按最新帖子的日期对类别ID数组排序,以便最新更新的系列可以自动浮动到列表的顶部

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

Show the category title, description and a url to it\'s archive by using it\'s category ID:

<获取类别标题:get_the_category_by_ID()category_description()get_category_link()

Get the Ids of the oldest and newest post in that category:

// Get oldest post ID
$posts = get_posts( array(
    \'category\' => 1,
    \'orderby\' => \'date\',
    \'order\' => \'ASC\',
    \'post_type\' => \'post\',
    \'post_status\' => \'publish\', 
) );
$oldest_post_id = $posts[0]->ID;
// Get newest post ID
$posts = get_posts( array(
    \'category\' => 1,
    \'orderby\' => \'date\',
    \'order\' => \'DESC\',
    \'post_type\' => \'post\',
    \'post_status\' => \'publish\', 
) );
$newest_post_id = $posts[0]->ID;

Sort an array of category ids by the date of the latest post:

function get_categories_by_the_date() {
    $categories_by_the_date = array();
    $categories = get_categories();

    foreach ( $categories as $category ) {
        $posts = get_posts( array(
            \'category\' => $category->term_id,
            \'orderby\' => \'date\',
            \'order\' => \'DESC\',
            \'post_type\' => \'post\',
            \'post_status\' => \'publish\', 
        ) );
        $categories_by_the_date[strtotime($posts[0]->post_date)] = $category->term_id;
    }

    krsort($categories_by_the_date);

    return array_values($categories_by_the_date);
}