Simple Form To Select A Category And Sort Order
There is a lot of code here, and the output doesn’t look very pretty, but it illustrates a basic form to let the Viewer select a Category and sort order for the Twenty Ten theme.
<?php
/*
Template Name: Simple-wp-Form
A template for Twenty Ten to display Posts
with a form for choosing category and sort order.
*/
global $wp_query;
$default_cat = 'All Categories';
$default_sort = 'Title, A to Z';
?>
<?php get_header(); ?><!-- file:simple-wp-form.php -->
<div id="container">
<div id="content">
<?php
// First, get any data from a previous display of the Page
if ($_GET['frmReset']) {
$reset_page = true;
$curr_cat = $default_cat;
$curr_sort = $default_sort;
} else {
$reset_page = ($_POST['frmSubmit'] == 'frmSubmit') ? true : false;
$curr_cat = ($_POST['frmCategory']) ? $_POST['frmCategory'] :
(($_GET['frmCategory']) ? $_GET['frmCategory'] : $default_cat);
//print_r('<p>GET CAT:' . $_GET['frmCategory'] . ' POST CAT:' . $_POST['frmCategory'] . " CURR CAT:$curr_cat" . '</p>');
$curr_sort = ($_POST['frmSort']) ? $_POST['frmSort'] :
(($_GET['frmSort']) ? $_GET['frmSort'] : $default_sort);
// print_r('<p>CURR SORT:');print_r($curr_sort);print_r('</p>');
}
// Set values for the dropdowns
$cats = get_terms('category');
$sort_opts = array(
'Title, A to Z' => array('orderby' => 'title', 'order' => 'ASC'),
'Title, Z to A' => array('orderby' => 'title', 'order' => 'DESC'),
);
?>
<?php
// Show the title from the calling Page and save the permalink
if (have_posts()) {
while (have_posts()) {
the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h1 class="entry-title"><?php the_title(); ?></h1>
</div>
<?php $page_permalink = get_permalink(); // save this for later use
break;
}
} ?>
<?php
// Set up the parameters to query the Posts
$args = array(
'ignore_sticky_posts' => 1,
);
$category = ($curr_cat == $default_cat) ? '' : $curr_cat;
if($category) $args['category_name'] = $category;
$sort = ($sort_opts[$curr_sort]) ? $sort_opts[$curr_sort] : array();
$args = array_merge($args,$sort);
// print_r('<p>ARGS:');print_r($args);print_r('</p>');
if ($reset_page || $_POST['frmSubmit'] == 'frmSubmit') {
$paged = 1;
} elseif (get_query_var('paged')) {
$paged = get_query_var('paged');
} elseif (get_query_var('page')) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
$args['paged'] = $paged;
// Issue the query so we can show the top pagination
$my_query = new WP_Query($args);
?>
<div class="simple-option-form">
<form method="post" action="<?php echo $page_permalink; ?>">
<table>
<tbody>
<tr>
<td align="right">Category:</td>
<td><select name="frmCategory">
<option value='All Categories'>All Categories</option>
<?php
foreach ($cats as $cat) {
$selected = ($cat->slug == $curr_cat) ? 'selected="selected"' : '';
echo "<option value='$cat->slug' $selected>$cat->name</option>n";
}
?>
</select></td>
<td width="30px"> </td>
<td align="right">Sort:</td>
<td><select name="frmSort">
<?php
foreach (array_keys($sort_opts) as $sort_key) {
$selected = ($sort_key == $curr_sort) ? 'selected="selected"' : '';
echo "<option value='$sort_key' $selected>$sort_key</option>n";
}
?>
</select></td>
<td width="30px"> </td>
<td><input type="submit" value="Submit" /></td>
</tr>
</tbody>
</table>
<input type="hidden" name="frmSubmit" value="frmSubmit" />
</form>
</div>
<?php
// Pagination ==============
global $wp_rewrite;
//figure out page
$current = $paged;
// TESTING ONLY - change page numbers
// $max_num_pages += 100;
// $current = 22;
$prev_text = __('« Previous Page');
$next_text = __('Next Page »');
$pagination_args = array(
'base' => @add_query_arg('paged','%#%'),
'format' => '',
'total' => $my_query->max_num_pages,
'current' => $current,
'show_all' => false,
'type' => 'array',
'prev_text' => $prev_text,
'next_text' => $next_text,
);
$add_args = array('frmCategory' => urlencode($curr_cat), 'frmSort' => urlencode($curr_sort) );
$pagination_args['add_args'] = $add_args;
// Create pagination string for later use
$links = paginate_links($pagination_args);
if( $links ) :
$sep = ' ';
$paginate_string = "<div class='paginate'>";
// Make sure 'Previous Page' and 'Next Page' always show
if ( stripos($links[0], $prev_text) === false ) array_unshift($links,"<span class='paginate-inactive'>$prev_text</span>");
$last_link = end($links);
reset($links);
if( stripos($last_link, $next_text) === false ) array_push($links, "<span class='paginate-inactive'>$next_text</span>");
// Now add Page x of Y to the front
$paginate_string .= " Page <span class='page-numbers current'>$current</span> of $my_query->max_num_pages: ";
for ($i = 0;$i < sizeof($links) ; ++$i ) {
if ($i > 0) $paginate_string .= $sep;
$link = $links[$i];
if( preg_match('#(>d+</)#',$link, $matches) ) {
$paginate_string .= preg_replace('#(>(d+)</)#', '>[ 2 ]</', $link );
} else {
$paginate_string .= $link;
}
}
$paginate_string .= "</div><!-- /paginate -->";
endif;
// End Pagination ============
// And finally, we get to show the Posts
if ( $my_query->have_posts() ) :
echo $paginate_string;
while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
<p><?php the_title(); echo "   Posted in $curr_cat"; ?></p>
<?php endwhile;
echo $paginate_string;
endif; ?>
</div><!-- #container -->
</div><!-- #content -->
<div class="cleared"></div>
<?php
// get_sidebar();
get_footer();
?>