我在页面模板上有一个表单,用于控制页面上显示的帖子以及这些帖子的排序方式。你可以理解我的意思:http://www.theseattlevine.com/the-vine
我的问题是,当用户使用底部的分页转到下一页或特定页时,如何让表单“记住”所选的值?
编辑:看起来像答案中的会话,但我无法让它工作。非常感谢您的帮助。
这是我在格式化并添加以下建议后的当前代码。它仍然不起作用。
<?php
/**
* Template Name: The Vine
*
* @package WordPress
* @subpackage Twenty_Eleven
* @since Twenty Eleven 1.0
*/ ?>
<?php
//Form Processing-----------------------------------------------------------------
//Display chosen number of posts per page
$gridperpage = array("8", "16", "32", "64", "96");
$listperpage = array("10", "20", "40", "65", "100");
$perpagecurrent = $_POST[\'perpage\'];
//If the number is already set, use it
if (isset($_POST[\'perpage\'])) {
$perpage = $_POST[\'perpage\'];
}
//If the number of posts matches the multiples of the grid numbers AND we\'re on the list page, switch the count to 20
if ((!in_array($perpagecurrent, $listperpage)) && (($_POST[\'view\']) == \'list\') ){
$perpage = \'20\';
}
//If we\'re on the grid page and the number is a multiple from the list page, switch to 32. Also do this if there is no value set yet since the default view is the grid
elseif (((!in_array($perpagecurrent, $gridperpage)) && (($_POST[\'view\']) == \'grid\')) || (!$_POST[\'perpage\']) ){
$perpage = \'32\';
}
//Display chosen post types
if ((!$_POST[\'userposts\']) && (!$_POST[\'posts\'])) :
$posttype= array(\'tsv_userpost\', \'post\');
elseif ((($_POST[\'userposts\']) == \'true\') && (($_POST[\'posts\']) == \'true\')) :
$posttype= array(\'tsv_userpost\', \'post\');
elseif ((($_POST[\'userposts\']) == \'true\') && (($_POST[\'posts\']) == \'\')) :
$posttype= \'tsv_userpost\';
elseif ((($_POST[\'userposts\']) == \'\') && (($_POST[\'posts\']) == \'true\')) :
$posttype= \'post\';
endif;
//Display only selected User Post Categories
if (isset($_POST[\'showvideos\']) && ($_POST[\'showvideos\'] == \'true\')) {
$cats[]=\'user-video\';
}
if (isset($_POST[\'showimages\']) && ($_POST[\'showimages\'] == \'true\')) {
$cats[]=\'user-image\';
}
if (isset($_POST[\'showaudio\']) && ($_POST[\'showaudio\'] == \'true\')) {
$cats[]=\'user-audio\';
}
if (isset($_POST[\'showwriting\']) && ($_POST[\'showwriting\'] == \'true\')) {
$cats[]=\'user-writing\';
}
if (isset($_POST[\'showevents\']) && ($_POST[\'showevents\'] == \'true\')) {
$cats[]=\'user-event\';
}
;
$upostcat = implode(\',\',$cats); //Put all the selected categories into the $upostcat array
?>
<?php
//Sort posts by selected method
if (isset($_POST[\'sortby\']) && ($_POST[\'sortby\'] == \'date\')) {
$sortmetakey=\'\';
$sortby=\'date\';
}
elseif (isset($_POST[\'sortby\']) && ($_POST[\'sortby\'] == \'rating\')) {
$sortmetakey=\'ratings_average\';
$sortby=\'meta_value_num date\';
}
elseif (isset($_POST[\'sortby\']) && ($_POST[\'sortby\'] == \'favscount\')) {
$sortmetakey=\'wpfp_favorites\';
$sortby=\'meta_value_num date\';
}
elseif (isset($_POST[\'sortby\']) && ($_POST[\'sortby\'] == \'viewcount\')) {
$sortmetakey=\'_count-views_all\';
$sortby=\'meta_value_num date\';
}
;
?>
<?php //Session
session_start();
// your formfields
$post_fields = array( \'sortby\', \'view\', \'perpage\' , \'posts\' , \'userposts\' , \'showaudio\' , \'showvideos\' , \'showimages\' , \'showevents\' , \'showwriting\' );
$form_data = array();
// copy needed form data from $_POST array
foreach ( $post_fields as $key )
if ( isset( $_POST[$key] ) ) $form_data[$key] = $_POST[$key];
// save your form data in a session if no form data was saved before
if ( ! empty( $form_data ) && ! isset( $_SESSION[\'form_data\'] )) $_SESSION[\'form_data\'] = serialize( $form_data );
// read saved form data from session
if ( isset( $_SESSION[\'form_data\'] ) && ! empty( $_SESSION[\'form_data\'] ) && empty( $form_data )) $form_data = unserialize( $_SESSION[\'form_data\'] );
?>
<?php
//Start of content
get_header();
?>
<div id="vine-menu">
<?php //Search the vine ?>
<div class="vine-search">
<form method="get" id="searchform" action="<?php echo esc_url( home_url( \'/\' ) ); ?>">
<label for="s" class="assistive-text"><?php _e( \'Search\', \'twentyeleven\' ); ?></label>
<input type="text" class="field" name="s" id="vinesearch" placeholder="<?php esc_attr_e( \'Search\', \'twentyeleven\' ); ?>" />
<input type="submit" class="submit" name="submit" id="searchsubmit" value="<?php esc_attr_e( \'Search\', \'twentyeleven\' ); ?>" />
</form>
</div>
<?php //Browse the vine ?>
<?php
//Start filter menu
?>
<form method="post" id="vine-sort-form" >
<?php
if ($_SERVER[\'REQUEST_METHOD\']!=\'POST\') {
$notsubmitted=\'checked\';
$notsubmitted_dd=\'selected\';
}
;
//Auto-select all post types when user first selects to show userposts
if (\'true\' == ($_POST[\'userposts\']) && (\'\' == ($_POST[\'showvideos\'])) && (\'\' == ($_POST[\'showaudio\'])) && (\'\' == ($_POST[\'showimages\'])) && (\'\' == ($_POST[\'showwriting\'])) && (\'\' == ($_POST[\'showevents\'])) ) {
$userpostselected=\'checked\';
}
;
//Dont allow both post types to go unselected
if (\'true\' != ($_POST[\'userposts\']) && (\'true\' != ($_POST[\'posts\']))) {
$noselectnone=\'checked\';
}
;
?>
<?php php//Choose View ?>
<?php
if (($_POST[\'view\'] == \'grid\') || (!isset($_POST[\'view\']))) { ?>
<input id="viewgrid" type="radio" name="view" value="grid" onclick="this.form.submit()" <?php
if (\'grid\' == ($_POST[\'view\'])) :
echo \'checked\';
elseif ($notsubmitted) :
echo $notsubmitted;
endif ?> style="display:none;">
<label class="imagelabel" id="gridchecked" for="viewgrid"><img src="/wordpress/image/icons/grid-on.png" /></label>
<?php } else { ?>
<input id="viewgrid" type="radio" name="view" value="grid" onclick="this.form.submit()" <?php
if (\'grid\' == ($_POST[\'view\'])) :
echo \'checked\';
elseif ($notsubmitted) :
echo $notsubmitted;
endif ?> style="display:none;">
<label class="imagelabel" id="gridunchecked" for="viewgrid"><img src="/wordpress/image/icons/grid-off.png" /></label>
<?php } ?>
<?php
if ($_POST[\'view\'] == \'list\') { ?>
<input id="viewlist" type="radio" name="view" value="list" onclick="this.form.submit()" <?php
if (\'list\' == ($_POST[\'view\'])) :
echo \'checked\';
endif ?> style="display:none;">
<label id="listchecked" class="imagelabel" for="viewlist"><img src="/wordpress/image/icons/list-on.png" /></label>
<?php } else { ?>
<input id="viewlist" type="radio" name="view" value="list" onclick="this.form.submit()" <?php
if (\'list\' == ($_POST[\'view\'])) :
echo \'checked\';
endif ?> style="display:none;">
<label id="listunchecked" class="imagelabel" for="viewlist"><img src="/wordpress/image/icons/list-off.png" /></label>
<?php } ?>
<br />
<div class="vine-menu-heading">Per page:</div>
<?php if ((($_POST[\'view\']) == \'grid\') || (!isset($_POST[\'perpage\']))) { ?>
<select name="perpage" id="perpage">
<option value="8" <?php
if (\'8\' == ($_POST[\'perpage\'])) :
echo \'selected="selected"\';
endif;
?> onclick="this.form.submit()">2 rows (8)</option>
<option value="16" <?php
if (\'16\' == ($_POST[\'perpage\'])) :
echo \'selected="selected"\';
endif;
?> onclick="this.form.submit()">4 rows (16)</option>
<option value="32" <?php
if (\'32\' == ($_POST[\'perpage\'])) :
echo \'selected="selected"\';
elseif ((in_array($perpagecurrent, $listperpage))) :
echo \'selected="selected"\';
elseif (!$_POST[\'perpage\']) :
echo \'selected="selected"\';
endif;
?> onclick="this.form.submit()">8 rows (32)</option>
<option value="64" <?php
if (\'64\' == ($_POST[\'perpage\'])) :
echo \'selected="selected"\';
endif;
?> onclick="this.form.submit()">16 rows (64)</option>
<option value="96" <?php
if (\'96\' == ($_POST[\'perpage\'])) :
echo \'selected="selected"\';
endif;
?> onclick="this.form.submit()">24 rows (96)</option>
</select>
<?php } else { ?>
<select name="perpage" id="perpage">
<option value="10" <?php
if (\'10\' == ($_POST[\'perpage\'])) :
echo \'selected="selected"\';
endif;
?> onclick="this.form.submit()">10 posts</option>
<option value="20" <?php
if (\'20\' == ($_POST[\'perpage\'])) :
echo \'selected="selected"\';
elseif ((in_array($perpagecurrent, $gridperpage))) :
echo \'selected="selected"\';
elseif (!$_POST[\'perpage\']) :
echo \'selected="selected"\';
endif;
?> onclick="this.form.submit()">20 posts</option>
<option value="40" <?php
if (\'40\' == ($_POST[\'perpage\'])) :
echo \'selected="selected"\';
endif;
?> onclick="this.form.submit()">40 posts</option>
<option value="65" <?php
if (\'65\' == ($_POST[\'perpage\'])) :
echo \'selected="selected"\';
endif;
?> onclick="this.form.submit()">65 posts</option>
<option value="100" <?php
if (\'100\' == ($_POST[\'perpage\'])) :
echo \'selected="selected"\';
endif;
?> onclick="this.form.submit()">100 posts</option>
</select>
<?php } ?>
<div class="vine-menu-heading">Sort by:</div>
<input type="radio" name="sortby" value="date" onclick="this.form.submit()" <?php
if (\'date\' == ($_POST[\'sortby\'])) :
echo \'checked\';
elseif ($notsubmitted) :
echo $notsubmitted;
endif;
?>>Date<br />
<input type="radio" name="sortby" value="rating" onclick="this.form.submit()" <?php
if (\'rating\' == ($_POST[\'sortby\'])) :
echo \'checked\';
endif ?>>Rating<br />
<input type="radio" name="sortby" value="favscount" onclick="this.form.submit()" <?php
if (\'favscount\' == ($_POST[\'sortby\'])) :
echo \'checked\';
endif ?>># of Favs<br />
<input type="radio" name="sortby" value="viewcount" onclick="this.form.submit()" <?php
if (\'viewcount\' == ($_POST[\'sortby\'])) :
echo \'checked\';
endif ?>># of Views<br />
<div class="vine-menu-heading">Post Type:</div>
<input type="checkbox" name="posts" value="true" onclick="this.form.submit()" <?php
if (\'true\' == ($_POST[\'posts\'])) :
echo \'checked\';
elseif ($notsubmitted) :
echo $notsubmitted;
elseif($noselectnone) :
echo $noselectnone;
endif;
?>>Featured Posts<br />
<input type="checkbox" name="userposts" value="true" onclick="this.form.submit()" <?php
if (\'true\' == ($_POST[\'userposts\'])) :
echo \'checked\';
elseif ($notsubmitted) :
echo $notsubmitted;
elseif($noselectnone) :
echo $noselectnone;
endif;
?>>User Posts<br />
<div class="user-cat-select">
<?php
if (\'true\' == ($_POST[\'userposts\'])) {
if (\'\' == ($_POST[\'posts\'])) {
?>
<input type="checkbox" name="showvideos" value="true" onclick="this.form.submit()" <?php
if (\'true\' == ($_POST[\'showvideos\'])) :
echo \'checked\';
elseif ($notsubmitted) :
echo $notsubmitted;
elseif ($userpostselected) :
echo $userpostselected;
endif;
?>>Video<br />
<input type="checkbox" name="showimages" value="true" onclick="this.form.submit()" <?php
if (\'true\' == ($_POST[\'showimages\'])) :
echo \'checked\';
elseif ($notsubmitted) :
echo $notsubmitted;
elseif ($userpostselected) :
echo $userpostselected;
endif;
?>>Images<br />
<input type="checkbox" name="showwriting" value="true" onclick="this.form.submit()" <?php
if (\'true\' == ($_POST[\'showwriting\'])) :
echo \'checked\';
elseif ($notsubmitted) :
echo $notsubmitted;
elseif ($userpostselected) :
echo $userpostselected;
endif;
?>>Writing<br />
<input type="checkbox" name="showaudio" value="true" onclick="this.form.submit()" <?php
if (\'true\' == ($_POST[\'showaudio\'])) :
echo \'checked\';
elseif ($notsubmitted) :
echo $notsubmitted;
elseif ($userpostselected) :
echo $userpostselected;
endif;
?>>Audio<br />
<input type="checkbox" name="showevents" value="true" onclick="this.form.submit()" <?php
if (\'true\' == ($_POST[\'showevents\'])) :
echo \'checked\';
elseif ($notsubmitted) :
echo $notsubmitted;
elseif ($userpostselected) :
echo $userpostselected;
endif;
?>>Events<br />
<?php
}
}
?>
</div>
</form>
</div><?php //.thevinemenu ?>
<div class="vine-display"><?php //.vine-display ?>
<?php
// The Query
$counter = 1;
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') :
1;
$the_query = new WP_Query( array(\'post_type\'=> $posttype,\'posts_per_page\'=>$perpage,\'paged\'=> $paged,\'category_name\'=>$upostcat,\'meta_key\' =>$sortmetakey, \'orderby\' =>$sortby, \'order\'=> \'DESC\'));
//Top Navigation
wp_pagenavi( array( \'query\' => $the_query ) );
//Start loop
while ( $the_query->have_posts() ) :
$the_query->the_post();
if (\'grid\' == ($_POST[\'view\']) || ($_SERVER[\'REQUEST_METHOD\']!=\'POST\')) :
get_template_part(\'the-vine\', \'grid\');
elseif (\'list\' == ($_POST[\'view\'])) :
get_template_part(\'the-vine\', \'list\');
endif;
?>
<?php endwhile; ?>
<div class="wp-page-navi-bottom"><?php wp_pagenavi( array( \'query\' => $the_query ) ); ?></div>
<?php wp_reset_query(); ?>
</div>
<?php get_footer(); ?>