【问题标题】:Wordpress search bar outputting only posts titlesWordpress 搜索栏仅输出帖子标题
【发布时间】:2018-02-04 10:35:21
【问题描述】:

我想在 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; ?>


content-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; ?>

【问题讨论】:

  • 您遇到了什么问题?你给了我们代码,但没有告诉我们它有什么问题,或者你已经做了什么来修复它。请查看How do I ask a good question
  • 能否将代码粘贴到 content.php 文件中,以便我为您提供帮助。
  • @RajendranNadar 你命名的 content.php 文件到底是什么?是 mypage.php 吗?我应该将所有代码粘贴到该文件中吗?
  • 您是在制作新主题还是现有主题。如果它是来自 wordpress 主题 repo 的主题,它将有一个名为 content.php 的文件来显示标题、联系方式和元数据等内容
  • 我正在开发自己构建的新主题。到目前为止,这三个文件是我必须使搜索栏正常工作的文件。

标签: php wordpress search posts


【解决方案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>

【讨论】:

    【解决方案2】:

    这是试图访问错误的模板文件

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-19
      • 1970-01-01
      • 2011-05-11
      • 2018-01-02
      • 1970-01-01
      • 1970-01-01
      • 2016-11-19
      • 1970-01-01
      相关资源
      最近更新 更多