2017-02-19 8 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 '); 

まだ動作していません。私はこれを行うために多くの方法を試みました。私はここに積み重なっています。私がデフォルトの検索ページを使用しているときには、非常にうまくいっています。ほとんど私はリダイレクトも必要です。私がやっている間違いはありますか、これを行うには他の方法がありますか? while ($the_query->have_posts()) : $the_query->the_post();

答えて

0

変更while ($the_query->have_posts()) : the_post();あなたはまた、このパスget_template_part('content', get_post_format());

+0

おかげで男正しいことを確認しthe_post();

$the_query->を逃したが、あなたはライン '$ the_query =新しいWP_Query($ argsを)を逃しました。 //検索クエリをテストしただけです。私はちょうどそれをテストしましたが、 '$ the_query->'を追加した後に動作しませんでした。 –

+0

'get_template_part'が正しいと確信していますか?そうでなければ 'the_title();'と置き換えてみてください。あなたのコードは私のために働き、 'the_post();'に '$ the_query->'を加え、 'get_template_part'を私のもので置き換えました。 – mattkrupnik

+0

実際には動作しません –

関連する問題