首页 文章

Wordpress搜索显示空结果的空白页

提问于
浏览
0

我必须整理一个自定义搜索,根据用户填写的表单将用户发送到不同的搜索结果页面,搜索也已设置为搜索自定义帖子类型 .

当搜索找到结果时,所有内容都会正确显示,但是当它执行空搜索时,它似乎不会运行content-none.php模板 . 而不是“抱歉,但没有与您的搜索字词匹配 . 请使用其他一些不同的关键字再试一次 . ”它只是一个带页眉和页脚的空白页面 .

例如,您可以转到:http://biozymeconnect.ev-labs.com/reseller/,在第二个输入中使用"tx"进行搜索结果,然后使用随机搜索没有结果的搜索 .

细节:

  • 基本主题:下划线

  • 框架:Zurb的基金会

使用此代码进行自定义帖子类型搜索更改http://adambalee.com/search-wordpress-by-custom-fields-without-a-plugin/

/**
 * Extend WordPress search to include custom fields
 *
 * http://adambalee.com
 */

/**
 * Join posts and postmeta tables
 *
 * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_join
 */
function cf_search_join( $join ) {
    global $wpdb;

    if ( is_search() ) {    
        $join .=' LEFT JOIN '.$wpdb->postmeta. ' ON '. $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id ';
    }

    return $join;
}
add_filter('posts_join', 'cf_search_join' );

/**
 * Modify the search query with posts_where
 *
 * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_where
 */
function cf_search_where( $where ) {
    global $pagenow, $wpdb;

    if ( is_search() ) {
        $where = preg_replace(
            "/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
            "(".$wpdb->posts.".post_title LIKE $1) OR (".$wpdb->postmeta.".meta_value LIKE $1)", $where );
    }

    return $where;
}
add_filter( 'posts_where', 'cf_search_where' );

/**
 * Prevent duplicates
 *
 * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_distinct
 */
function cf_search_distinct( $where ) {
    global $wpdb;

    if ( is_search() ) {
        return "DISTINCT";
    }

    return $where;
}
add_filter( 'posts_distinct', 'cf_search_distinct' );

要自定义搜索结果,我使用了本教程http://wpgarage.com/code-snippets/how-to-customize-multiple-search-result-pages-in-wordpress/

编辑了search.php

<?php
/* Template Name: Search Results */
$search_refer = $_GET["site_section"];
if ($search_refer == 'reseller') { load_template(TEMPLATEPATH . '/reseller-search.php'); }
elseif ($search_refer == 'site-search') { load_template(TEMPLATEPATH . '/dealer-search.php'); }; ?>

我编辑了reseller-search.php和dealer-search.php .

<?php
/**
 * The template for displaying search results pages.
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/#search-result
 *
 * @package Biozyme_Connect
 */

get_header(); ?>

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

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

            <header class="page-header">
                <h1 class="page-title"><?php printf( esc_html__( 'Search Results for: %s', 'bzconnect' ), '<span>' . get_search_query() . '</span>' ); ?></h1>
            </header><!-- .page-header -->
            <span style="background-color:#f87d20; display:block; padding:4px; color:#ffffff; font-weight:bold; text-transform: uppercase;">*Please note that you will be required to login after clicking the link to receive your discounted rates.</span>
            <table width="100%" class="search-results">
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Company</th>
                        <th>State</th>
                        <th>Order</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <?php
                        /* Start the Loop */
                        while ( have_posts() ) : the_post();

                            /**
                             * Run the loop for the search to output the results.
                             * If you want to overload this in a child theme then include a file
                             * called content-search.php and that will be used instead.
                             */
                            get_template_part( 'template-parts/content', 'search-reseller' );

                        endwhile;
                        echo '</tr>
                </tbody>
            </table>';

                        the_posts_navigation();

                    else :

                        get_template_part( 'template-parts/content', 'none' );

                    endif; ?>

        </main><!-- #main -->
    </section><!-- #primary -->

<?php
get_footer();
?>

content-none.php代码

<?php
/**
 * Template part for displaying a message that posts cannot be found.
 *
 * @link https://codex.wordpress.org/Template_Hierarchy
 *
 * @package Biozyme_Connect
 */

?>

<section class="no-results not-found">
    <header class="page-header">
        <h1 class="page-title"><?php esc_html_e( 'Nothing Found', 'bzconnect' ); ?></h1>
    </header><!-- .page-header -->

    <div class="page-content">
        <?php
        if ( is_home() && current_user_can( 'publish_posts' ) ) : ?>

            <p><?php printf( wp_kses( __( 'Ready to publish your first post? <a href="%1$s">Get started here</a>.', 'bzconnect' ), array( 'a' => array( 'href' => array() ) ) ), esc_url( admin_url( 'post-new.php' ) ) ); ?></p>

        <?php elseif ( is_search() ) : ?>

            <p><?php esc_html_e( 'Sorry, but nothing matched your search terms. Please try again with some different keywords.', 'bzconnect' ); ?></p>
            <?php
                get_search_form();

        else : ?>

            <p><?php esc_html_e( 'It seems we can&rsquo;t find what you&rsquo;re looking for. Perhaps searching can help.', 'bzconnect' ); ?></p>
            <?php
                get_search_form();

        endif; ?>
    </div><!-- .page-content -->
</section><!-- .no-results -->

1 回答

  • 0

    在查看共享用于搜索和检查'view-source'的链接之后,似乎根本没有加载 content-none 模板,因为即使HTML没有显示在 div.main 中 .

    我现在可以想到两个可能导致这个问题的原因:

    1.检查以确保您确实进入 else: 状态 . 虽然这不太可能,但确定是好的 . 所以通过做类似的事情来检查它

    else :
                    echo 'Nothing found!';
                    get_template_part( 'template-parts/content', 'none' );
    
                endif; ?>
    

    检查您是否可以在页面上看到 Nothing found! .

    • 确保content-none.php文件确实存在,即

    一个 . 它位于正确的目录中,例如 template-parts/content-none.php ,只需确保我们处于正确的路径中

    湾文件名是正确的,例如它不是 cotent-none.phpcontent-noen.php 或类似的东西 .

相关问题