当用户访问搜索结果页面时,用户需要单击"清除搜索过滤器",这会将用户重定向到搜索页面。此操作时将清除会话。
我正在寻找一种通过 ajax 销毁会话的方法 - 以便用户可以直接在搜索结果页面上使用过滤器选项(而无需返回搜索页面)。
这是我现在拥有的代码:
<?php session_start(); if($_GET['action']=='reset') {
session_destroy();
wp_redirect( 'http://website.com/search' ); exit;
} else {
if(!is_page(4097)) {
session_destroy(); }
} ?>
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
</head>
<body>
<?php if(!is_page(4097)) { ?>
<form name="search-form" method="post" action="<?php echo get_permalink(4097);?>">
<select name="one">
<?php echo $form_options_one; ?>
</select>
<select name="two">
<?php echo $form_options_two; ?>
</select>
<input type="submit" name="submit" value="Search">
</form>
<?php }?>
<div id="container">
search_and_results.php代码如下:
<?php
session_start();
get_header();
if($_GET['action']=='reset'){
session_destroy();
wp_redirect( get_permalink(get_the_ID()) ); exit;
}?>
<div id="content">
<?php if(!isset($_POST['submit']) && (!isset($_SESSION['one'])) && (!isset($_SESSION['two']))) { ?>
<form name="search-form" method="post" action="<?php echo get_permalink(4097);?>">
<select name="one">
<?php echo $form_options_one; ?>
</select>
<select name="two">
<?php echo $form_options_two; ?>
</select>
<input type="submit" name="submit" value="Search">
</form>
<?php } else {
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$the_query = new WP_Query($args); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php $author_data= get_userdata($post->post_author); $author_meta= get_user_meta($post->post_author); ?>
<?php if(($author_data->roles[0]=='editor') || ($author_data->roles[0]=='contributor') || ($author_data->roles[0]=='subscriber') ) { ?>
// only get posts from editors, contributors, and subscribers
<!--post stuff here-->
<?php }?>
<?php endwhile; ?> <!-- end of the loop -->
<!-- pagination here -->
<?php if($the_query->max_num_pages==1) {
session_destroy();
}?>
<?php if ( $the_query->max_num_pages > 1 ) : ?>
<div class="main_navigation">
<?php
// usage with max_num_pages
next_posts_link( 'Next Page »', $the_query->max_num_pages );
previous_posts_link( '« Previous Page' );
?>
</div>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
<div class="back-to-form clearfix">
<a href="<?php echo home_url('/') ?>?action=reset" class="clear_filter button">< Clear search filters</a>
</div>
<?php } ?>
</div><!-- end content id -->
</div><!--container-->
</body>
</html>
鉴于上述结构,这可能吗?非常感谢
据我所知,您需要更改构建页面的方式来执行此操作。首先创建一个外部页面以进行 AJAX 调用并处理该外部页面中的会话。例如,每次此外部页面加载会话时,都像这样设置为 false
external-page.php
<?php
$_SESSION['searchfilters']=0;
//this will set to 0 any time this page is run by ajax or original url.
?>
其次,如果您想加载搜索页面而无需用户离开页面,请通过 jquery 或 ajax 进行另一次调用,将您的搜索页面加载到指定的div 或某个 HTML 标记中。
你的 ajax 函数是这样的
$.ajax({
type:"POST",
//data:'',
url:"http://www.yourdomain.com/external-page.php",//this will clear the session values to 0.
success: function(data){
//here you reload your search page into designated div with an id or class then user do not have to leave the page.
}, //end of success
error: function(){
//display any error message here
}// end of error
});
希望这能帮助你找到自己的道路。
提醒一下,请勿使用 session_destroy(),如果您的网站上有用户拥有帐户,这将终止当前用户的所有现有会话并将其注销。