我正在开发一个项目,在这个项目中,我必须显示与特定类别名称相关的所有帖子。
我已经搜索了很多,但我没有任何想法来实现这个。
如何才能显示特定类别/术语中的所有帖子
我正在开发一个项目,在这个项目中,我必须显示与特定类别名称相关的所有帖子。
我已经搜索了很多,但我没有任何想法来实现这个。
如何才能显示特定类别/术语中的所有帖子
仅使用WP_Query()
要生成自定义查询,using the category parameters.
假设您知道(或知道如何获取)特定类别的ID,如$catid
:
<?php
$category_query_args = array(
\'cat\' => $catid
);
$category_query = new WP_Query( $category_query_args );
?>
注意:您也可以传递类别slug 至查询,通过category_name
, 而不是cat
.现在,只需输出循环:
<?php
if ( $category_query->have_posts() ) : while $category_query->have_posts() : $category_query->the_post();
// Loop output goes here
endwhile; endif;
?>
下面的代码将从特定类别名称中获取文章标题。
<?php
$myposts = get_posts(array(
\'showposts\' => 8, //add -1 if you want to show all posts
\'post_type\' => \'your-post-type\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'your-taxonomy\',
\'field\' => \'slug\',
\'terms\' => \'term-name\' //pass your term name here
)
))
);
foreach ($myposts as $mypost) {
// echo $mypost->post_title . \'<br/>\';
// echo $mypost->post_content . \'<br/>\';
// echo $mypost->ID . \'<br/><br/>\';
echo \'<li class="faq"> <p class="title"><a href="\' . get_permalink($mypost) . \'">\' . $mypost->post_title . \'</a></p></li>\';} ?>
WP_Query
\'stax_query
是实现这一目标最灵活的方式。如果你把问题说得更具体一点,我应该可以为你编写一些示例代码,让你继续学习。
您可以使用插件(WordPress Category Posts) 为此。
WordPress类别帖子是WordPress的插件,它创建特定类别帖子的链接列表。
无论您想在何处列出某个类别的帖子,请使用以下代码:
wp_cat_posts(get_cat_ID(\'your_category_name\'));
非常感谢。 query_posts(\'category_name=my_category_name&showposts=5\');
while (have_posts()) : the_post();
get_the_content();
endwhile;
我知道这是一个真正的新手问题,但我似乎无法从帖子中获得循环。它所做的只是从页面本身中提取。我制作了一个模板并添加了循环。<?php if( have_posts() ) { while( have_posts() ) { the_post(); ?> <h2><?php the_title(); ?></h2> <?php } } ?>