Wordpress 搜索栏只输出帖子标题 [英] Wordpress search bar outputting only posts titles

查看:64
本文介绍了Wordpress 搜索栏只输出帖子标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

非常感谢您的帮助!

searchform.php

<form role="search" method="get" class="search-form" action="<?php echo home_url( '/' ); ?>"><标签><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' ) ?>"/><input type="submit" class="search-submit" value="<?php echo esc_attr_x('搜索', '提交按钮') ?>"/>


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/* 翻译器:%s:搜索查询.*/printf( esc_html__( 'Search Results for: %s', 'materialpress' ), '' . get_search_query() . '' );?></h1></header><!-- .page-header --><?phpwhile ( have_posts() ) : the_post();/* 确保模板是你的 content.php */get_template_part('内容');终了;the_posts_navigation();别的 :/* 显示没有找到内容的页面 */echo '未找到帖子';万一;?></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="容器"><div id="content_post"><div class="col-lg-12 col-md-12 col-sm-12 col-xs-12"><?php the_content();?>

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



content-search.php

<?php get_header();?><?php if (have_posts()): ?><div class="容器"><?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>

<?php endwhile;?>

<?php endif;?>

解决方案

问题在于 content-search.php 还包含一个标题和一个循环.通过在 content-search.php 中使用类似的内容,它应该只包含您希望为主循环中的每个项目重用的代码部分:

 

<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>

I would like to create a Wordpress search bar in PHP, displayed in just one page of my site (I am working on a new theme I have made). As result, the search bar should output only the permalinks (titles) of my posts that are related to the search keyword.
The issue I have now is that the search output is showing lorem ipsum text and the sidebar. Whatever keywords I search, that same output is showing up. (see screenshot).

Thank you very much for your help!

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

解决方案

The problem is that content-search.php also contains a header and a loop. It should only contain the sections of code you want to reuse for each item in the main loop, by using something like this in 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>

这篇关于Wordpress 搜索栏只输出帖子标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆