Wordpress搜索栏仅输出帖子标题

时间:2017-08-26 16:02:35

标签: php wordpress search posts

我想在PHP中创建一个Wordpress搜索栏,只显示在我网站的一个页面中(我正在制作一个新主题)。因此,搜索栏应仅输出与搜索关键字相关的帖子的永久链接(标题)。
我现在的问题是搜索输出显示lorem ipsum文本和侧边栏。无论我搜索什么关键字,都会显示相同的输出。 (见截图)。

非常感谢您的帮助!

searchform.php

<form role="search" method="get" class="search-form" action="<?php echo home_url( '/' ); ?>">
  <label>
    <span class="screen-reader-text"><?php echo _x( 'Search for:', 'label' ) ?></span>
        <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' ) ?>" />
  </label>
  <input type="submit" class="search-submit" value="<?php echo esc_attr_x( 'Search', 'submit button' ) ?>" />
</form>


的search.php

<?php get_header(); ?>

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

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

        <header class="page-header">
          <h1 class="page-title"><?php
            /* translators: %s: search query. */
            printf( esc_html__( 'Search Results for: %s', 'materialpress' ), '<span>' . get_search_query() . '</span>' );
          ?></h1>
        </header><!-- .page-header -->

        <?php
        while ( have_posts() ) : the_post();
          /* Make sure the template is your content.php */
          get_template_part('content');

        endwhile;

        the_posts_navigation();

      else :
        /* Show no content found page */
        echo 'Not posts found';

      endif; ?>

      </main><!-- #main -->
    </section><!-- #primary -->

<?php get_sidebar(); get_footer();


mypage.php

<?php get_header(); ?>


<?php get_search_form(); ?>

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
  <div class="container">
    <div id="content_post">
      <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
        <?php the_content(); ?>
      </div>
    </div>    
  </div>

<?php endwhile; ?>
<?php endif; ?>


内容的search.php
<?php get_header(); ?>

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

<div class="container">
  <?php while(have_posts()): the_post(); ?>
  <div class="row-posts-container">
    <div class="row-posts">
      <div class="posts col-lg-12 hidden-sm col-md-12 hidden-xs">
        <a href="<?php the_permalink(); ?>"><?php $first = str_replace(' | ', '<br />', get_the_title()); echo str_replace('REPLACE_ME', '<i>REPLACE_ME</i>', $first);?></a>
      </div>
    </div>
  </div>
  <?php endwhile; ?>
</div>

<?php endif; ?>

enter image description here

2 个答案:

答案 0 :(得分:1)

问题是 content-search.php 还包含标题和循环。它应该只包含你想要为主循环中的每个项重用的代码部分,在 content-search.php 中使用类似的东西:

  <div class="row-posts-container">
    <div class="row-posts">
      <div class="posts col-lg-12 hidden-sm col-md-12 hidden-xs">
        <a href="<?php the_permalink(); ?>"><?php $first = str_replace(' | ', '<br />', get_the_title()); echo str_replace('REPLACE_ME', '<i>REPLACE_ME</i>', $first);?></a>
      </div>
    </div>

答案 1 :(得分:0)

这是否正在尝试访问错误的模板文件

while ( have_posts() ) : the_post();
     /* Make sure the template is your content.php */
      get_template_part('content', 'search');
endwhile;

您需要这样做,因为文件名是content-search.php