2017-08-26 52 views
2

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

非常感谢您的帮助!

searchform.phpWordpress搜索栏只输出帖子标题

<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

+0

什么是您所遇到的问题?你给了我们代码,但没有告诉我们问题是什么,或者你已经做了什么来解决它。请回顾[我如何问一个好问题](https://stackoverflow.com/help/how-to-ask)。 – FluffyKitten

+0

您可以将代码粘贴到content.php文件中,以便我可以帮助您。 –

+0

@RajendranNadar这是什么命名content.php文件?它是mypage.php吗?我应该在该文件中粘贴所有代码吗? – Lolo

回答

1

问题是,content-search.php也包含一个头和一个循环。它应该只包含要重用在主循环每一个项目的代码的部分,通过使用这样的事情在内容的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> 
+0

谢谢!它与此有关peferctly! :) – Lolo

0

做这个尝试访问一个错误的模板文件

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

你需要这样做,因为该文件名是内容的search.php

相关问题