当前位置: 首页 > 知识库问答 >
问题:

如何显示Wordpress搜索结果?

公冶森
2023-03-14

我已经花了很多时间弄清楚为什么我的搜索在我定制的模板中不起作用。到目前为止,我已经知道了如何包含searchform。php文件在我的头,创建搜索。php文件目前是空的(因此,当我搜索某个内容时,我会被重定向到一个空白页面,我想我肯定需要search.php文件中的某些内容才能使其正常工作),我阅读了Wordpress codex的所有内容,但找不到解决方案,我找到的唯一有用信息是这个。

http://codex.wordpress.org/Creating_a_Search_Page

为了显示搜索结果,您能建议需要做些什么吗?是否有特殊的查询、函数等?我真的到处都找不到。

我的searchform.php文件,以防你需要。

<form action="<?php echo home_url(); ?>" id="search-form" method="get">
    <input type="text" name="s" id="s" value="type your search" onblur="if(this.value=='')this.value='type your search'"
    onfocus="if(this.value=='type your search')this.value=''" />
    <input type="hidden" value="submit" />
</form>

共有3个答案

金兴朝
2023-03-14

我使用的searchform.phpsearch.php文件已经提到,但在这里我提供了实际的代码

创建搜索页面codex页面在此提供帮助,而创建搜索页面\u模板显示搜索查询。

在我的例子中,我将$search_query参数传递给WP_QueryClass(它可以确定是否是搜索查询!)。然后,我运行循环来显示我想要的帖子信息,在我的例子中,这是the_permalink和the_title

搜索框表格:

<form class="search" method="get" action="<?php echo home_url(); ?>" role="search">
  <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' ) ?>" />
  <button type="submit" role="button" class="btn btn-default right"/><span class="glyphicon glyphicon-search white"></span></button>
</form>

搜索。php模板文件:

<?php
    global $query_string;
    $query_args = explode("&", $query_string);
    $search_query = array();

    foreach($query_args as $key => $string) {
      $query_split = explode("=", $string);
      $search_query[$query_split[0]] = urldecode($query_split[1]);
    } // foreach

    $the_query = new WP_Query($search_query);
    if ( $the_query->have_posts() ) : 
    ?>
    <!-- the loop -->

    <ul>    
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <li>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        </li>   
    <?php endwhile; ?>
    </ul>
    <!-- end of the loop -->

    <?php wp_reset_postdata(); ?>

<?php else : ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
松亦
2023-03-14

基本上,您需要在搜索中包含Wordpress循环。php模板循环搜索结果并将其显示为模板的一部分。

下面是WordPress主题搜索模板和ThemeShaper页面模板中的一个非常基本的示例。

<?php
/**
 * The template for displaying Search Results pages.
 *
 * @package Shape
 * @since Shape 1.0
 */

get_header(); ?>

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

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

                <header class="page-header">
                    <h1 class="page-title"><?php printf( __( 'Search Results for: %s', 'shape' ), '<span>' . get_search_query() . '</span>' ); ?></h1>
                </header><!-- .page-header -->

                <?php shape_content_nav( 'nav-above' ); ?>

                <?php /* Start the Loop */ ?>
                <?php while ( have_posts() ) : the_post(); ?>

                    <?php get_template_part( 'content', 'search' ); ?>

                <?php endwhile; ?>

                <?php shape_content_nav( 'nav-below' ); ?>

            <?php else : ?>

                <?php get_template_part( 'no-results', 'search' ); ?>

            <?php endif; ?>

            </div><!-- #content .site-content -->
        </section><!-- #primary .content-area -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>
熊锐进
2023-03-14

你需要包括Wordpress循环在你的search.php这是例子

搜索php模板文件:

<?php get_header(); ?>
<?php
$s=get_search_query();
$args = array(
                's' =>$s
            );
    // The Query
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
        _e("<h2 style='font-weight:bold;color:#000'>Search Results for: ".get_query_var('s')."</h2>");
        while ( $the_query->have_posts() ) {
           $the_query->the_post();
                 ?>
                    <li>
                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                    </li>
                 <?php
        }
    }else{
?>
        <h2 style='font-weight:bold;color:#000'>Nothing Found</h2>
        <div class="alert alert-info">
          <p>Sorry, but nothing matched your search criteria. Please try again with some different keywords.</p>
        </div>
<?php } ?>

<?php get_sidebar(); ?>
<?php get_footer(); ?>
 类似资料:
  • 我的分页工作正常,但是当使用搜索组件时,它只显示结果的第一页。我的页面URL没有搜索看起来像:http://localhost:8000/dictionary-management/postcode?page=2它的工作正确。 带搜索的我的第一页URL:http://localhost:8000/dictionary-管理/邮政编码/搜索和它的工作正常。 带搜索的我的第二页URL:http://l

  • 我正在使用批量请求执行弹性搜索完整索引。我在索引过程中遇到了一个问题,结果是空的。由于我正在完整索引期间删除索引,因此如何处理这种情况。 我已经完成了以下步骤: 删除索引 创建索引 创建映射 批量请求 索引属性和映射: } 我有大约7.5万份文件。 谢谢,Sree。

  • 问题内容: 当前,我正在使用Lucene 3.0.2版创建类似于字典的搜索应用程序。我要显示的对象之一是一种“示例”,其中Lucene将在书中寻找一个单词,然后显示使用该单词的句子。 我一直在阅读《 Lucene in Action》一书,其中提到了类似的内容,但通过浏览我找不到其他提及。这是Lucene可以做的吗?如果是,您该怎么办? 问题答案: 我相信您正在寻找的是荧光笔。 一种可能是使用lu

  • 问题内容: 如何突出显示使用php的mysql查询的搜索结果? 这是我的 [修改] 代码: 问题答案: 您可以使用preg_replace();,当它在文本中找到匹配项时,您可以在匹配词周围放置一个带有突出显示类别的div。然后,您可以向突出显示类添加背景颜色和边框,以使其突出显示 preg_replace期望3个参数; 第一个是您要寻找的 第二个是应该更改为 他应从中搜索并替换的文本字符串 例如

  • 输入出发地和抵达地,可搜索前往目的地的路径。 轻触(选项)>[搜索路径],在出发地与抵达地的字段各自输入关键词并轻触[搜索]。地图会显示最短路径。 可从书签中选择出发地及抵达地。请轻触(书签),选择地点。 A ) (徒步)/(乘车) 可选择路径的移动方式。轻触即会以选择的移动方式搜索最短路径。 B ) 与目的地间的距离及所需时间 C ) (显示列表)/(隐藏列表) 轻触即可将搜索结果列表切换为显示

  • 想搜索地点时可输入相关的关键词或住址来搜索地点。 轻触(选项)>[搜索地点],在搜索栏输入关键词后轻触[搜索]。搜索结果会在地图上以(地点旗帜)显示。 查看搜索结果列表 显示地点旗帜时,可查看搜索结果列表。 1. 显示地图的状态下轻触屏幕。 2. 轻触(显示列表)。 画面会显示搜索结果列表。轻触想显示的地点即会显示地图。