Php WordPress自定义搜索页面不工作
我正在创建一个新的WordPress网站。试图创建自定义搜索页面,但当我要搜索任何内容时,它不会显示任何结果。但是查询仍然有帖子Php WordPress自定义搜索页面不工作,php,wordpress,Php,Wordpress,我正在创建一个新的WordPress网站。试图创建自定义搜索页面,但当我要搜索任何内容时,它不会显示任何结果。但是查询仍然有帖子 $s=get_search_query(); $args = array( 's' =>$s ); // The Query $the_query = new WP_Query( $args ); // Just tested the search query get_header(); ?> <div class
$s=get_search_query();
$args = array(
's' =>$s
);
// The Query
$the_query = new WP_Query( $args ); // Just tested the search query
get_header();
?>
<div class="main_content">
<div class="row">
<div class="col-sm-12">
<div class="custom-breadcrumb">
<h1>News Digest ― <?php printf( __( '%s', 'decouvr' ), get_search_query() ); ?></h1>
<?php
schema_breadcrumb();
?>
</div>
</div>
</div>
</div>
<div class="container container-most">
<div class="row">
<?php
if ( $the_query->have_posts() ) :
// Start the loop.
while ( $the_query->have_posts() ) : the_post();
get_template_part( 'content', get_post_format() );
// End the loop.
endwhile;
// Previous/next page navigation.
docuvr_paging_nav();
// If no content, include the "No posts found" template.
else :
get_template_part( 'content', 'none' );
endif;
?>
</div>
</div>
<?php
get_footer();
?>
它仍然不起作用。我已经尝试了很多方法来做到这一点。我就在这里。还有一件事,当我使用默认的搜索页面时,它的工作非常好。主要是我也需要重定向。我做错了什么,或者有其他方法吗?Changewhile($the_query->have_posts()):the_post()
onwhile($the_query->have_posts()):$the_query->the_post()
您错过了帖子()中的$查询->
代码>
还要检查此路径是否get_template_part('content',get_post_format())代码>是正确的谢谢你,伙计,但是你错过了一行
$the_query=new WP_query($args);//刚刚测试了搜索查询
。我刚刚测试了它,但是在添加了$the\u query->
之后,它就不工作了。你确定get\u template\u part
正确吗?如果没有,请尝试将其替换为标题()代码>例如。你的代码对我有用,只是在the_post()中添加了$the_query->
编码>并将get_template_part
替换为my one,correct one。同时从add_操作('template_redirect','search_url_rewrite')中删除空格代码>应该像add_操作('template_redirect','search_url_rewrite')代码>这就是页面无法重定向的原因。
function search_url_rewrite () {
if ( is_search() && !empty( $_GET['s'] ) ) {
wp_redirect( home_url( '/news/' ) . urlencode( get_query_var( 's' ) ) );
exit();
}
}
add_action( 'template_redirect', ' search_url_rewrite ' );