2013-02-10 15 views
26

मैंने बहुत समय बिताया है कि मेरी खोज मेरे कस्टम टेम्पलेट में क्यों काम नहीं कर रही है। अब तक मैं यह पता लगाने में सक्षम था कि मेरे हेडर में searchform.php फ़ाइल को कैसे शामिल किया जाए, खोजा गया है। Php फ़ाइल जो वर्तमान में खाली है (इसलिए जब मैं किसी चीज़ की खोज करता हूं तो मुझे रिक्त पृष्ठ पर रीडायरेक्ट किया जाता है और मुझे लगता है कि मैं निश्चित रूप से इसे काम करने के लिए search.php फ़ाइल में कुछ चाहिए), मैं वर्डप्रेस कोडेक्स के चारों ओर पढ़ रहा था लेकिन समाधान नहीं मिला, केवल मुझे मिली उपयोगी जानकारी यह थी।वर्डप्रेस खोज परिणामों को कैसे प्रदर्शित करें?

http://codex.wordpress.org/Creating_a_Search_Page

आप बता सकते हैं क्या जरूरत के लिए एक खोज के परिणामों को प्रदर्शित करने के लिए किया जाना? क्या कोई विशेष प्रश्न, फ़ंक्शन इत्यादि है? मैं वास्तव में इसे कहीं भी नहीं ढूंढ सकता।

मेरी searchform.php फ़ाइल की आवश्यकता होने पर फ़ाइल।

<form action="<?php echo home_url(); ?>" id="search-form" method="get"> 
    <input type="text" name="s" id="s" value="type your search" onblur="if(this.value=='')this.value='type your search'" 
    onfocus="if(this.value=='type your search')this.value=''" /> 
    <input type="hidden" value="submit" /> 
</form> 
+0

के रूप में ही नहीं है इस [इस अन्य प्रश्न?] (Http://stackoverflow.com/questions/14800675/wordpress-not-displaying-search-results) –

+0

@DamienPirsy यह था, मैं पिछले प्रश्न हटा दिया गया क्योंकि लोगों को समझने में भ्रमित हो गया, मैंने इसे एक आसान बना दिया। – Ilja

उत्तर

16

मूल रूप से, आप खोज परिणामों के माध्यम से लूप करने के लिए अपने search.php टेम्पलेट में Wordpress पाश में शामिल हैं और उन्हें टेम्पलेट के भाग के रूप दिखाने की जरूरत है।

नीचे थीमशपर पर The WordPress Theme Search Template and Page Template से एक बहुत ही बुनियादी उदाहरण है।

<?php 
/** 
* The template for displaying Search Results pages. 
* 
* @package Shape 
* @since Shape 1.0 
*/ 

get_header(); ?> 

     <section id="primary" class="content-area"> 
      <div id="content" class="site-content" role="main"> 

      <?php if (have_posts()) : ?> 

       <header class="page-header"> 
        <h1 class="page-title"><?php printf(__('Search Results for: %s', 'shape'), '<span>' . get_search_query() . '</span>'); ?></h1> 
       </header><!-- .page-header --> 

       <?php shape_content_nav('nav-above'); ?> 

       <?php /* Start the Loop */ ?> 
       <?php while (have_posts()) : the_post(); ?> 

        <?php get_template_part('content', 'search'); ?> 

       <?php endwhile; ?> 

       <?php shape_content_nav('nav-below'); ?> 

      <?php else : ?> 

       <?php get_template_part('no-results', 'search'); ?> 

      <?php endif; ?> 

      </div><!-- #content .site-content --> 
     </section><!-- #primary .content-area --> 

<?php get_sidebar(); ?> 
<?php get_footer(); ?> 
4

चेक theme फ़ोल्डर में अपने टेम्पलेट search.php और searchform.php या नहीं होता है या नहीं।

9

मैं पहले से ही उल्लेख किए गए searchform.php और search.php फ़ाइलों का उपयोग कर रहा हूं, लेकिन यहां मैं वास्तविक कोड प्रदान करता हूं।

Creating a Search Pagecodex पृष्ठ यहां सहायता करता है और #Creating_a_Search_Page_Template खोज क्वेरी दिखाता है।

मेरे मामले में मैं WP_QueryClass करने के लिए $search_query तर्क पारित (जो खोज क्वेरी का है निर्धारित कर सकते हैं!)। मैं तो पोस्ट जानकारी मैं चाहता हूँ, जो मेरे मामले में the_permalink और the_title है प्रदर्शित करने के लिए The Loop चलाते हैं।

खोज बॉक्स प्रपत्र:

<form class="search" method="get" action="<?php echo home_url(); ?>" role="search"> 
    <input type="search" class="search-field" placeholder="<?php echo esc_attr_x('Search …', 'placeholder') ?>" value="<?php echo get_search_query() ?>" name="s" title="<?php echo esc_attr_x('Search for:', 'label') ?>" /> 
    <button type="submit" role="button" class="btn btn-default right"/><span class="glyphicon glyphicon-search white"></span></button> 
</form> 

search.php टेम्पलेट फ़ाइल:

<?php 
    global $query_string; 
    $query_args = explode("&", $query_string); 
    $search_query = array(); 

    foreach($query_args as $key => $string) { 
     $query_split = explode("=", $string); 
     $search_query[$query_split[0]] = urldecode($query_split[1]); 
    } // foreach 

    $the_query = new WP_Query($search_query); 
    if ($the_query->have_posts()) : 
    ?> 
    <!-- the loop --> 

    <ul>  
    <?php while ($the_query->have_posts()) : $the_query->the_post(); ?> 
     <li> 
      <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> 
     </li> 
    <?php endwhile; ?> 
    </ul> 
    <!-- end of the loop --> 

    <?php wp_reset_postdata(); ?> 

<?php else : ?> 
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p> 
<?php endif; ?> 
21

आप अपने search.php में Wordpress पाश शामिल करने की ज़रूरत यह है उदाहरण

search.php टेम्पलेट फ़ाइल:

<?php get_header(); ?> 
<?php 
$s=get_search_query(); 
$args = array(
       's' =>$s 
      ); 
    // The Query 
$the_query = new WP_Query($args); 
if ($the_query->have_posts()) { 
     _e("<h2 style='font-weight:bold;color:#000'>Search Results for: ".get_query_var('s')."</h2>"); 
     while ($the_query->have_posts()) { 
      $the_query->the_post(); 
       ?> 
        <li> 
         <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> 
        </li> 
       <?php 
     } 
    }else{ 
?> 
     <h2 style='font-weight:bold;color:#000'>Nothing Found</h2> 
     <div class="alert alert-info"> 
      <p>Sorry, but nothing matched your search criteria. Please try again with some different keywords.</p> 
     </div> 
<?php } ?> 

<?php get_sidebar(); ?> 
<?php get_footer(); ?> 
+0

धन्यवाद। इससे मेरा काम बनता है। मैं खोज परिणाम के लिए संख्या भी गिनना चाहता हूं। जैसे 4 खोज परिणाम मिले। कोई विचार कैसे? pic तरह – pagol

+0

नए कोड नए variabe जोड़ें: http://i.imgur.com/8NgI15D.png –

+0

की जांच करने के परिणाम गिनती इस का उपयोग करें:: < इस तस्वीर है?वैश्विक $ wp_query; $ total_results = $ wp_query-> found_posts; ?>। प्रलेखन: https://codex.wordpress.org/Creating_a_Search_Page –

संबंधित मुद्दे