【问题标题】:How to display Wordpress search results?如何显示 Wordpress 搜索结果?
【发布时间】:2013-01-25 23:39:08
【问题描述】:

我花了很多时间弄清楚为什么我的搜索在我的自定义模板中不起作用。到目前为止,我能够弄清楚如何在我的标题中包含 searchform.php 文件,创建了当前为空的 search.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>

【问题讨论】:

  • 这和this other question?不一样
  • @DamienPirsy 是的,我删除了上一个问题,因为结果让人难以理解,我把这个问题简化了。

标签: php wordpress search


【解决方案1】:

您需要在 search.php 中包含 Wordpress 循环 这是例子

search.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(); ?>

【讨论】:

  • 谢谢。这个对我有用。我还想计算搜索结果的数量。喜欢 4 个搜索结果。知道怎么做吗?
  • 新代码如图片添加新变量:这是图片:i.imgur.com/8NgI15D.png
  • 要检查结果计数,请使用:found_posts; ?> .文档:codex.wordpress.org/Creating_a_Search_Page
  • 不检查就以这种方式使用输出安全吗?
  • @RodinOleksandr 你可以随意修改代码和添加验证码
【解决方案2】:

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

以下是来自 ThemeShaper 的The WordPress Theme Search Template and Page Template 的一个非常基本的示例。

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

【讨论】:

    【解决方案3】:

    如前所述,我正在使用searchform.phpsearch.php 文件,但这里我提供了实际代码。

    Creating a Search Pagecodex 页面在这里提供帮助,#Creating_a_Search_Page_Template 显示搜索查询。

    在我的例子中,我将$search_query 参数传递给WP_Query Class(它可以确定是否是搜索查询!)。然后我运行The Loop 来显示我想要的帖子信息,在我的例子中是the_permalinkthe_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>
    

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

    【讨论】:

      【解决方案4】:

      检查theme文件夹中的模板是否包含search.phpsearchform.php

      【讨论】:

        【解决方案5】:

        WordPress 在搜索结果中包含标签、类别和分类法

        此代码取自http://atiblog.com/custom-search-results/

        这里的一些功能取自二十九主题。因为它是在这个主题上制作的。

        此代码示例将帮助您在搜索中包含标签、类别或任何自定义分类。并显示包含这些标签或类别的帖子。

        您需要修改您的主题的 search.php 才能这样做。

        <?php
        $search=get_search_query();
        $all_categories = get_terms( array('taxonomy' => 'category','hide_empty' => true) ); 
        $all_tags = get_terms( array('taxonomy' => 'post_tag','hide_empty' => true) );
        //if you have any custom taxonomy
        $all_custom_taxonomy = get_terms( array('taxonomy' => 'your-taxonomy-slug','hide_empty' => true) );
        
        $mcat=array();
        $mtag=array();
        $mcustom_taxonomy=array();
        
        foreach($all_categories as $all){
        $par=$all->name;
        if (strpos($par, $search) !== false) {
        array_push($mcat,$all->term_id);
        }
        }
        
        foreach($all_tags as $all){
        $par=$all->name;
        if (strpos($par, $search) !== false) {
        array_push($mtag,$all->term_id);
        }
        }
        
        foreach($all_custom_taxonomy as $all){
        $par=$all->name;
        if (strpos($par, $search) !== false) {
        array_push($mcustom_taxonomy,$all->term_id);
        }
        }
        
        $matched_posts=array();
        $args1= array( 'post_status' => 'publish','posts_per_page' => -1,'tax_query' =>array('relation' => 'OR',array('taxonomy' => 'category','field' => 'term_id','terms' =>$mcat),array('taxonomy' => 'post_tag','field' => 'term_id','terms' =>$mtag),array('taxonomy' => 'custom_taxonomy','field' => 'term_id','terms' =>$mcustom_taxonomy)));
        
        $the_query = new WP_Query( $args1 );
        if ( $the_query->have_posts() ) {
        while ( $the_query->have_posts() ) {
        $the_query->the_post();
        array_push($matched_posts,get_the_id());
        //echo '<li>' . get_the_id() . '</li>';
        }
        wp_reset_postdata();
        } else {
        
        }
        
        ?>
        <?php
        // now we will do the normal wordpress search
        $query2 = new WP_Query( array( 's' => $search,'posts_per_page' => -1 ) );
        if ( $query2->have_posts() ) {
        while ( $query2->have_posts() ) {
        $query2->the_post();
        array_push($matched_posts,get_the_id());
        }
        wp_reset_postdata();
        } else {
        
        }
        $matched_posts= array_unique($matched_posts);
        $matched_posts=array_values(array_filter($matched_posts));
        //print_r($matched_posts);
        ?>
        
        <?php
        $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
        $query3 = new WP_Query( array( 'post_type'=>'any','post__in' => $matched_posts ,'paged' => $paged) );
        if ( $query3->have_posts() ) {
        while ( $query3->have_posts() ) {
        $query3->the_post();
        get_template_part( 'template-parts/content/content', 'excerpt' );
        }
        twentynineteen_the_posts_navigation();
        wp_reset_postdata();
        } else {
        
        }
        ?>
        

        【讨论】:

          猜你喜欢
          • 2013-01-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-03-01
          • 2011-10-12
          • 1970-01-01
          相关资源
          最近更新 更多