简体   繁体   English

如何根据页面中的类别显示wordpress帖子

[英]How to display wordpress post according to category in pages

I'm working on a site.The problem is i have to display the categorized posts in their respective pages. 我在一个网站上工作。问题是我必须在各自的页面中显示分类的帖子。 I'm using a custom theme that I'm working on. 我正在使用一个正在处理的自定义主题。 basically I'm making a site in WordPress. 基本上我是用WordPress建立网站。 Links to other helpful tutorials on making will be awesome. 指向其他有用的制作教程的链接将非常棒。

This is the code you need to add in your webpage. 这是您需要在网页中添加的代码。 Replace 1 with the category id you want. 将1替换为所需的类别ID。

<?php
$posts = get_posts(array('category' => 1));
?>

You can refer this link for more detailed info. 您可以参考链接以获取更多详细信息。

Edit: More detailed info. 编辑:更详细的信息。

<?php
$category = get_the_category(); 
echo $category[0]->cat_name;
?>

Now $category is you array of category. 现在$category是您的类别数组。 you can get the category name by $category[$i]->cat_name So, you can search your category in array and get it's "id". 您可以通过$category[$i]->cat_name获取类别名称。因此,您可以在数组中搜索类别并获取其“ id”。 and from first code you can get posts. 从第一个代码中您可以获取帖子。

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

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