2017-02-19 97 views
0

我正在創建一個新的WordPress網站。嘗試創建自定義搜索頁面,但是當我要搜索任何內容時,它不會顯示任何結果。但該查詢仍然有帖子。WordPress自定義搜索頁面無法正常工作

$s=get_search_query(); 
    $args = array(
     's' =>$s 
    ); 
// The Query 
$the_query = new WP_Query($args); // Just tested the search query 

get_header(); 
?> 

<div class="main_content"> 
    <div class="row"> 
     <div class="col-sm-12"> 
      <div class="custom-breadcrumb"> 
       <h1>News Digest &horbar; <?php printf(__('%s', 'decouvr'), get_search_query()); ?></h1> 
       <?php 
       schema_breadcrumb(); 
       ?> 
      </div> 
     </div> 
    </div> 
</div> 

<div class="container container-most"> 

    <div class="row"> 

     <?php 

     if ($the_query->have_posts()) : 
      // Start the loop. 
      while ($the_query->have_posts()) : the_post(); 
       get_template_part('content', get_post_format()); 

       // End the loop. 
      endwhile; 

      // Previous/next page navigation. 
      docuvr_paging_nav(); 
     // If no content, include the "No posts found" template. 
     else : 
      get_template_part('content', 'none'); 
     endif; 
     ?> 

    </div> 

</div> 

<?php 
get_footer(); 
?> 

我需要在用戶嘗試搜索時重定向搜索頁面。我用一個函數來重定向搜索頁面

function search_url_rewrite() { 
    if (is_search() && !empty($_GET['s'])) { 
     wp_redirect(home_url('/news/') . urlencode(get_query_var('s'))); 
     exit(); 
    } 
} 

add_action('template_redirect', ' search_url_rewrite '); 

它仍然沒有工作。我嘗試了很多方法來做到這一點。我只是堆在這裏。當我使用默認的搜索頁面時,還有一件事情非常好。大多數情況下我也需要重定向。是否有任何錯誤我正在做或有沒有其他方式來做到這一點?

回答

0

while ($the_query->have_posts()) : $the_query->the_post();變化while ($the_query->have_posts()) : the_post();the_post();

錯過$the_query->還要檢查這個路徑get_template_part('content', get_post_format());是正確的

+0

感謝的人,但是你錯過了行'$ the_query =新WP_Query($參數); //只測試了搜索查詢。我剛剛測試過,但是在它不工作之前添加'$ the_query->'之後。 –

+0

你確定'get_template_part'是否正確?如果不嘗試用'the_title();'替換它。你的代碼適用於我,只需在'the_post();'中添加'$ the_query->',並用我的一個正確的替換'get_template_part'。 – mattkrupnik

+0

它實際上不工作 –

相關問題