仅显示来自一个子类别的文章 - Wordpress
问题描述:
假设我有以下几类。仅显示来自一个子类别的文章 - Wordpress
Movies
----- Action
----- Science fiction
----- Drama
Music
----- POP
----- Rock
这里,电影和音乐是父类别,其余的是子类别。
如果我只想显示音乐类别中的POP音乐(POP的帖子),我该怎么做。
请帮忙!
我不知道如何显示子类别的帖子。只有我有一个示例代码,可以显示父和子类别的帖子。
<?
// Get the post ID
$id = get_the_ID();
//get all the categories (this function will return all categories for a post in an array)
$category= get_the_category($id);
if ($category->category_parent == 0) {
//extract the first category from the array
$catID = $category[0]->cat_ID;
//Assign the category ID into the query
$verticalNavigationSwitcher = "cat=$catID&orderby=ID&order=ASC";
}
$result = new WP_Query($verticalNavigationSwitcher);
//$featuredPosts->query('showposts=5&cat=3');
while ($result->have_posts()) : $result->the_post();
?>
<li><a href='<?php the_permalink() ?>'><span><?php the_title(); ?></span></a></li>
<?php
endwhile;
wp_reset_postdata();
?>
答
我已经找到一种方法,只显示一个子类别的职位。
如果我在查询中使用“category__in”,它将返回一个类别的帖子。
$verticalNavigationSwitcher = "category__in=$catID&orderby=ID&order=ASC";
答
看起来你正在使用的例子使事情复杂化。如果您只需要来自特定类别的帖子,则可以使用类别slug或ID来检索帖子,并使用WP_Query,如上例所示。因此,如果'Pop'类别的ID为4(您可以通过将鼠标悬停在Posts - > Categories ...上的类别名称中来获取类别ID),那么您的代码将会是;
<?php
$result = new WP_Query('cat=4');
while ($result->have_posts()) : $result->the_post();
?>
以下是食典相关部分;
https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
举一个例子你如何显示一般结果,对于不熟悉WordPress – 2013-04-22 13:11:57
的人显示你的代码。 – Rikesh 2013-04-22 13:16:01
嗨Royal和Rikesh,我添加了一个示例代码。目前我不知道如何显示子类别的帖子。此代码将显示来自类别的所有帖子及其子类别。 – Anam 2013-04-22 13:31:01