php - WordPress 从空搜索中排除搜索页面

标签 php 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 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

最佳答案

基本上,根据我的理解,您希望这样,如果用户进入搜索页面,他们就不会在页面上看到“搜索结果:”的标题,而冒号后没有任何内容。我有一个建议的代码更改,它将消除标题,并允许您向用户添加一条消息来搜索某些内容,以显示结果。

<?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”)有关,这实际上是一个标记为“搜索”的 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>

保留下面的几行,并将上面的行更改为我所拥有的。这将检查您的循环是否实际上是包含搜索结果的循环。如果是,则绘制结果,如果不是,则显示以下消息。

关于php - WordPress 从空搜索中排除搜索页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20723570/

相关文章:

vba - 在过滤器中使用 "Or"

php - 为任何 Wordpress 的 feed/rss 返回 404

php - 如何通过他们的 API 获取 UPS Delivery Progress?

仅适用于本地开发人员的 PHP、postfix、sendmail、thunderbird

php - PHP 和 JS 之间的数据通信

algorithm - 在比 O((k+N) * 2^(N/2)) 更快的范围内生成所有子集总和?

php - 检查 javascript 是否可用,如果 javascript 不可用则重定向

linux - 如何使用 locate 在 Linux 中进行部分搜索?

javascript - 如何使用 jQuery 或 CSS 设置提交按钮第一个单词的样式

php - 自定义帖子类型搜索应该适用于前端而不是管理员