【问题标题】:Filter WordPress search results between different (custom) post types在不同(自定义)帖子类型之间过滤 WordPress 搜索结果
【发布时间】:2016-06-03 12:55:19
【问题描述】:

我有一个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)

【问题讨论】:

  • `

    ()

  • 效果很好!你能把这个作为答案发布吗?所以我可以批准它

标签: php wordpress search


【解决方案1】:

您只需在循环中显示帖子post_type

<h1><?php the_title(); ?> (<?php echo get_post_type(); ?>)</h1>

理想情况下,您应该在while 循环结束后添加wp_reset_postdata();,以保持页面底部清洁:

<?php while( $the_query->have_posts() ){
    $the_query->the_post(); ?>
    <h1><?php the_title(); ?> (<?php echo get_post_type(); ?>)</h1>
<?php } 
wp_reset_postdata();

【讨论】:

    猜你喜欢
    • 2014-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-02
    相关资源
    最近更新 更多