2013-12-21 123 views
2

我創建了一個搜索頁面,並使用基於用戶輸入的循環返回結果。問題是當我進入搜索頁面時,當用戶沒有輸入任何內容時,它顯示搜索頁面標題。有沒有辦法從結果中刪除搜索頁面?我試過插件,但他們不工作 - 我確信我的循環有問題,但我沒看到它。WordPress的空搜索排除搜索頁面

我的代碼是:

<?php 
/* 
Template Name: Search Page 
?> 
<?php get_header(); ?> 

<section id="post-<?php the_title(); ?>" class="search-section left <?php post_class(); ?>" role="main"> 

    <div class="search-input"> 
     <form action="<?php echo home_url(); ?>" method="get" role="search" class="search-big"> 
      <input type="text" name="s" id="s" class="search-big" placeholder="Type in your search and press enter..." /> 
     </form> 
    </div> 

    <div class="search-result"> 

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

      <h2><?php if(!empty($_GET)) { printf(__('Search results for: %s', 'xma'), '<span>' . get_search_query() . '</span>'); } ?></h2> 

      <?php while (have_posts()) : the_post(); ?> 

       <div class="search-single-result"> 

       <a href="<?php the_permalink(); ?>"><h4><?php the_title(); ?></h4> 
       <p><?php the_excerpt(); ?></p></a> 
       </div> 

      <?php endwhile; ?> 

     <?php else : ?> 

      <h3><?php _e('Sorry, we couldn\'t find anything that matched your search.', 'xma'); ?></h3> 

     <?php endif; ?> 
    </div> 

</section> 
<aside class="search-sidebar right"> 
    <?php if (is_active_sidebar('sidebar-4')) : ?> 
     <?php dynamic_sidebar('sidebar-4'); ?> 
    <?php endif; ?> 

</aside> 

錯誤,多數民衆贊成的截圖發生:

screenshot of bug

回答

2
從我的理解

基本上,你這樣,如果用戶去希望在搜索頁面上,他們沒有在冒號後面看到名爲「搜索結果:」的頁面上的標題。我有一個建議的代碼更改,將消除標題,並允許您爲用戶輸入消息來搜索某些內容,以顯示結果。

<?php 
/* 
Template Name: Search Page 
?> 
<?php get_header(); ?> 

<section id="post-<?php the_title(); ?>" class="search-section left <?php post_class(); ?>" role="main"> 

    <div class="search-input"> 
    <form action="<?php echo home_url(); ?>" method="get" role="search" class="search-big"> 
     <input type="text" name="s" id="s" class="search-big" placeholder="Type in your search and press enter..." /> 
    </form> 
    </div> 

    <div class="search-result"> 

    <?php /* ADD THIS CODE */> 
    <?php $search_term = get_search_query(); ?> 
    <?php if (!empty($search_term)): /* if search term is not empty */ ?> 
    <?php /* END ADD THIS CODE */> 

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

     <h2><?php if(!empty($_GET)) { printf(__('Search results for: %s', 'xma'), '<span>' . get_search_query() . '</span>'); } ?></h2> 

     <?php while (have_posts()) : the_post(); ?> 

      <div class="search-single-result"> 
      <a href="<?php the_permalink(); ?>"><h4><?php the_title(); ?></h4> 
      <p><?php the_excerpt(); ?></p></a> 
      </div> 

     <?php endwhile; ?> 

     <?php else : ?> 

     <h3><?php _e('Sorry, we couldn\'t find anything that matched your search.', 'xma'); ?></h3> 

     <?php endif; ?> 

    <?php /* ADD THIS CODE */> 
    <?php else: /* if search term is empty */ ?> 
     <p>Please enter some search text to display search results.</p> 
    <?php endif; ?> 
    <?php /* END ADD THIS CODE */> 

    </div> 

</section> 
<aside class="search-sidebar right"> 
    <?php if (is_active_sidebar('sidebar-4')) : ?> 
    <?php dynamic_sidebar('sidebar-4'); ?> 
    <?php endif; ?> 
</aside> 

有兩個代碼塊要添加,一個頂部,一個低。希望這可以幫助!

EDIT(後澄清)

我們談過之後,它看起來像這個問題是有關的事實,你有搜索頁面的自定義URL,像「/搜索」,這實際上是一個標記爲「搜索」的WordPress頁面。問題是,當你加載這個搜索頁面時,你的循環已經有一個結果了,即當前頁面。您需要做的是首先檢查您的當前循環是用於搜索,還是用於顯示搜索頁面。這樣做是這樣的:

<?php 
/* 
Template Name: Search Page 
?> 
<?php get_header(); ?> 

<section id="post-<?php the_title(); ?>" class="search-section left <?php post_class(); ?>" role="main"> 

    <div class="search-input"> 
    <form action="<?php echo home_url(); ?>" method="get" role="search" class="search-big"> 
     <input type="text" name="s" id="s" class="search-big" placeholder="Type in your search and press enter..." /> 
    </form> 
    </div> 

    <div class="search-result"> 

    <?php /* CHANGE THESE LINES FROM ABOVE, KEEP LOWER LINES */ ?> 
    <?php global $wp_query; ?> 
    <?php if (!empty($wp_query->query_vars['s'])): ?> 
    <?php /* END CHANGE THESE LINES FROM ABOVE, KEEP LOWER LINES */ ?> 

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

     <h2><?php if(!empty($_GET)) { printf(__('Search results for: %s', 'xma'), '<span>' . get_search_query() . '</span>'); } ?></h2> 

     <?php while (have_posts()) : the_post(); ?> 

      <div class="search-single-result"> 
      <a href="<?php the_permalink(); ?>"><h4><?php the_title(); ?></h4> 
      <p><?php the_excerpt(); ?></p></a> 
      </div> 

     <?php endwhile; ?> 

     <?php else : ?> 

     <h3><?php _e('Sorry, we couldn\'t find anything that matched your search.', 'xma'); ?></h3> 

     <?php endif; ?> 

    <?php /* ADD THIS CODE */> 
    <?php else: /* if search term is empty */ ?> 
     <p>Please enter some search text to display search results.</p> 
    <?php endif; ?> 
    <?php /* END ADD THIS CODE */> 

    </div> 

</section> 
<aside class="search-sidebar right"> 
    <?php if (is_active_sidebar('sidebar-4')) : ?> 
    <?php dynamic_sidebar('sidebar-4'); ?> 
    <?php endif; ?> 
</aside> 

保持較低的行,並將這些頂部行改爲我所擁有的。這將檢查您的循環實際上是否具有搜索結果的循環。如果是,則會繪製結果,如果不是,則會顯示下面的消息。

+0

沒有基本上我已經添加了空if語句來解決這個問題 - 我需要實現的是從顯示中刪除頁面搜索。我添加了屏幕截圖,以便您可以看到發生了什麼 – jolen

+0

標題「搜索」是實際搜索頁面的鏈接,當用戶首次單擊「搜索」頁面時會發生這種情況。 – jolen

+0

我明白了。讓我思考一下 – loushou

1
Try this: 

    <div class="search-input"> 
    <form action="<?php echo home_url(); ?>" method="get" role="search" class="search-big"> 
    <input type="text" name="s" id="s" class="search-big" placeholder="Type in your search and press enter..." /> 
    </form> 
    </div> 

    <div class="search-result"> 
    <h2><?php if(!empty($_GET)) { printf(__('Search results for: %s', 'xma'),  '<span>' . get_search_query() . '</span>'); } ?></h2> 

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

     <?php while (have_posts()) : the_post(); ?> 

      <div class="search-single-result"> 

      <a href="<?php the_permalink(); ?>"><h4><?php the_title(); ?></h4> 
      <p><?php the_excerpt(); ?></p></a> 
      </div> 

     <?php endwhile; ?> 

    <?php else : ?> 

     <h3><?php _e('Sorry, we couldn\'t find anything that matched your search.', 'xma'); ?></h3> 
+0

嘗試這樣做,它返回一個白色的頁面錯誤。 – jolen

1

試試這個方法,我發現WordPress StackExchange

$exclude_pages = array('music', 'contact'); 
$exclude_post_types = array('music', 'any_here'); 

if (have_posts()) : while (have_posts()) : the_post(); 
if (is_page() && ! empty($exclude_pages) && (
in_array($post->post_name, (array)$exclude_pages) || 
in_array($post->post_title, (array)$exclude_pages) 
) || 
(is_single() && in_array(get_post_type(), (array)$exclude_post_types))) continue; 

讓我知道,如果它的工作原理,或者需要改進