我想创建一个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; ?>
什么是您所遇到的问题?你给了我们代码,但没有告诉我们问题是什么,或者你已经做了什么来解决它。请回顾[我如何问一个好问题](https://stackoverflow.com/help/how-to-ask)。 – FluffyKitten
您可以将代码粘贴到content.php文件中,以便我可以帮助您。 –
@RajendranNadar这是什么命名content.php文件?它是mypage.php吗?我应该在该文件中粘贴所有代码吗? – Lolo