简体   繁体   中英

Wordpress: get last post belonging to a category, when listing categories

I've tried looking at the database but I can't seem to be able to make a query to do this. How can I look up posts from only one category, or more specifically, I just need the ID of the last post from the category.

I'm trying to do this in index.php

Using get_posts will do what you need, documentation here

something along the lines of the following

<?php
  $postsincat = get_posts(array("cat" => 1, "showposts" => 1));
  $idoflatestpostincategory = $postsincat[0]->ID;
?>

You could also use category_name instead of cat in the array to use a name instead of ID.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM