我有一个search.php文件,它使用以下代码显示用户搜索的搜索结果:
<?php
$s=get_search_query();
$args = array('s' =>$s);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) { ?>
<?php while ( $the_query->have_posts() ) {
$the_query->the_post();
?>
<h1><?php the_title(); ?></h1>
<?php }
}else{ ?>
<h1>No search results found</h1>
<?php }
?>
有没有办法可以在不同的(自定义)帖子类型之间过滤结果?
像这样:
- item1 (post)
- item2 (custom post type named 'clients')
- item3 (custom post type named 'clients')
- item4 (custom post type named 'occasions')
- item5 (post)
答案 0 :(得分:2)
您只需在循环中显示帖子post_type
:
<h1><?php the_title(); ?> (<?php echo get_post_type(); ?>)</h1>
理想情况下,您应该在wp_reset_postdata();
循环结束后添加while
,以保持页面底部清洁:
<?php while( $the_query->have_posts() ){
$the_query->the_post(); ?>
<h1><?php the_title(); ?> (<?php echo get_post_type(); ?>)</h1>
<?php }
wp_reset_postdata();