從我的理解
基本上,你這樣,如果用戶去希望在搜索頁面上,他們沒有在冒號後面看到名爲「搜索結果:」的頁面上的標題。我有一個建議的代碼更改,將消除標題,並允許您爲用戶輸入消息來搜索某些內容,以顯示結果。
<?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>
保持較低的行,並將這些頂部行改爲我所擁有的。這將檢查您的循環實際上是否具有搜索結果的循環。如果是,則會繪製結果,如果不是,則會顯示下面的消息。
沒有基本上我已經添加了空if語句來解決這個問題 - 我需要實現的是從顯示中刪除頁面搜索。我添加了屏幕截圖,以便您可以看到發生了什麼 – jolen
標題「搜索」是實際搜索頁面的鏈接,當用戶首次單擊「搜索」頁面時會發生這種情況。 – jolen
我明白了。讓我思考一下 – loushou