【发布时间】: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’t find what you’re looking for. Perhaps searching can help.', 'bzconnect' ); ?></p>
<?php
get_search_form();
endif; ?>
</div><!-- .page-content -->
</section><!-- .no-results -->
【问题讨论】:
-
define('WP_DEBUG', true );你试过调试吗?
-
谢谢我以前没用过。我可以看到它在哪里引发了错误,我为页面或页面的子项添加了条件语句。
<?php elseif ($post->post_parent == 136) : ?> <?php wp_nav_menu( array( 'theme_location' => 'primary', 'container' => '', 'menu_class' => '', 'menu_id' => '', 'items_wrap' => '%3$s', 'depth' => 2 ) ); ?>和<?php elseif ($post->post_parent == 136) : ?> <?php wp_nav_menu( array( 'theme_location' => 'primary', 'container' => '', 'menu_class' => '', 'menu_id' => '', 'items_wrap' => '%3$s', 'depth' => 2 ) ); ?> -
我以基本的
<?php else : ?> <?php echo ''; ?> <?php endif; ?>结尾,所以我认为它会默认为那个。 -
好的,这个 [link]wordpress.stackexchange.com/questions/115390/… 为我指出了正确的方向,以消除我遇到的对象错误,但它仍然只是显示一个白页。
-
还从您的
template-parts/content-none.php文件中添加代码