admin管理员组文章数量:1394396
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
1 Answer
Reset to default 1Show 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);
}
本文标签: categoriesHow can you access category information from a theme
版权声明:本文标题:categories - How can you access category information from a theme? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744612292a2615730.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论