简体   繁体   English

WordPress:列出类别时,获取属于该类别的最新帖子

[英]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. 如何只查找一个类别的帖子,或更具体地说,我只需要该类别的最后一个帖子的ID。

I'm trying to do this in index.php 我正在尝试在index.php中执行此操作

Using get_posts will do what you need, documentation here 使用get_posts您的需求, 此处提供文档

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. 您也可以在数组中使用category_name代替cat来使用名称代替ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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