我创建了一个搜索页面,并根据用户输入使用循环返回结果。问题是,当我进入搜索页面时,当用户没有输入任何内容时,它会显示搜索页面标题。有没有办法可以从搜索结果中删除搜索页面?我已经尝试了插件但它们不起作用 - 我确信我的循环有问题但是我没有看到它。
我的代码是:
<?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>
发生错误的屏幕截图:
答案 0 :(得分: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>
要添加两个代码块,一个在顶部,一个在低位。希望这有帮助!
编辑(澄清后)
在我们讨论之后,看起来这个问题与您拥有搜索页面的自定义URL有关,例如'/ search',实际上是一个标有'Search'的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>
保持较低的线条,并将这些顶线改为我所拥有的。这将检查您的循环实际上是具有搜索结果的循环。如果是,则绘制结果,如果没有,则显示下面的消息。
答案 1 :(得分: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>
答案 2 :(得分: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;
让我知道它是否有效,或需要改进