Я создаю новый сайт 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="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();
?>
Мне нужно перенаправить страницу поиска, когда пользователь пытается найти. Я использовал функцию, чтобы перенаправить страницу поиска
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 ' );
Его все еще не работает. Я перепробовал много способов сделать это. Я просто сложен здесь. Есть одна вещь, когда я использую страницу поиска по умолчанию, она работает очень хорошо. В основном мне тоже нужен редирект. Есть ли что-то неправильно, что я делаю, или есть ли другой способ сделать это?
+ Изменить while ( $the_query->have_posts() ) : the_post();
на while ( $the_query->have_posts() ) : $the_query->the_post();
Вы пропустили $the_query->
в the_post();
Также проверьте, что этот путь get_template_part( 'content', get_post_format() );
верно
Других решений пока нет …