【问题标题】:Wordpress Search showing a blank page for empty resultsWordpress 搜索显示空结果的空白页面
【发布时间】:2016-05-29 14:23:48
【问题描述】:

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

当搜索找到一个结果时,一切都会正确显示,但是当它进行空搜索时,它似乎没有运行 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 -->

【问题讨论】:

  • define('WP_DEBUG', true );你试过调试吗?
  • 谢谢我以前没用过。我可以看到它在哪里引发了错误,我为页面或页面的子项添加了条件语句。 &lt;?php elseif ($post-&gt;post_parent == 136) : ?&gt; &lt;?php wp_nav_menu( array( 'theme_location' =&gt; 'primary', 'container' =&gt; '', 'menu_class' =&gt; '', 'menu_id' =&gt; '', 'items_wrap' =&gt; '%3$s', 'depth' =&gt; 2 ) ); ?&gt;&lt;?php elseif ($post-&gt;post_parent == 136) : ?&gt; &lt;?php wp_nav_menu( array( 'theme_location' =&gt; 'primary', 'container' =&gt; '', 'menu_class' =&gt; '', 'menu_id' =&gt; '', 'items_wrap' =&gt; '%3$s', 'depth' =&gt; 2 ) ); ?&gt;
  • 我以基本的&lt;?php else : ?&gt; &lt;?php echo ''; ?&gt; &lt;?php endif; ?&gt; 结尾,所以我认为它会默认为那个。
  • 好的,这个 [link]wordpress.stackexchange.com/questions/115390/… 为我指出了正确的方向,以消除我遇到的对象错误,但它仍然只是显示一个白页。
  • 还从您的template-parts/content-none.php 文件中添加代码

标签: php wordpress search


【解决方案1】:

查看您为搜索共享的链接并检查“查看源”后,似乎根本没有加载 content-none 模板,因为即使是 HTML 也没有显示在 div.main 中。

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

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

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

            endif; ?>

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

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

    一个。它在正确的目录中,例如template-parts/content-none.php,只要确保我们在正确的路径中

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

【讨论】:

  • 我不敢相信它是什么。 content-none.php 模板在那里,但当我仔细观察时,我意识到它加载了 0Kb 的文件大小,所以它没有读取任何内容。谢谢。
猜你喜欢
  • 2019-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-08
  • 2011-10-16
  • 2016-06-25
相关资源
最近更新 更多