最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

categories - How can you access category information from a theme?

programmeradmin3浏览0评论

I am making a wordpress site to host my artwork and comics, each comic series is in its own category, so I would like to display the category information in the site through my theme/a plugin I make.

There are three things I would like to do:

  • Show the category Title, Description and a url to it's archive by using it's category ID.
  • Get the Ids of the oldest and newest post in that category so I can add links to "Start Reading" and "Read the latest"
  • Sort an array of category ids by the date of the latest post, so the most recently updated series can automatically float to the top of the list.

Is it possible to do this?

I am making a wordpress site to host my artwork and comics, each comic series is in its own category, so I would like to display the category information in the site through my theme/a plugin I make.

There are three things I would like to do:

  • Show the category Title, Description and a url to it's archive by using it's category ID.
  • Get the Ids of the oldest and newest post in that category so I can add links to "Start Reading" and "Read the latest"
  • Sort an array of category ids by the date of the latest post, so the most recently updated series can automatically float to the top of the list.

Is it possible to do this?

Share Improve this question asked Apr 1, 2020 at 10:05 metichimetichi 1375 bronze badges 5
  • Yes, these all things are possible. – BlueSuiter Commented Apr 1, 2020 at 10:47
  • Could you point me to the documentation so I can read about it? Im having trouble figuring out how to google it. – metichi Commented Apr 1, 2020 at 10:59
  • What are the things you are done implementing? Or can I see your implementation? – BlueSuiter Commented Apr 1, 2020 at 11:14
  • I haven't implemented anything about this yet since so far I'm trying to figure out what functions to call to get the information – metichi Commented Apr 1, 2020 at 11:15
  • This one will fetch all the terms for you, developer.wordpress/reference/functions/get_terms – BlueSuiter Commented Apr 1, 2020 at 11:17
Add a comment  | 

1 Answer 1

Reset to default 1

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

  • Get category title: get_the_category_by_ID()
  • Get category description (ID optional): category_description()
  • Get category archive URL: 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);
}
发布评论

评论列表(0)

  1. 暂无评论