Let’s imagine you got a dropdown like the following
// The template tag that you place in your ... template
function WPSE116198PerPageSelect()
{
$selected =
<?php
<select name="woo_per_page" id="woo_per_page" size="3">
<?php
foreach ( array( 24, 36, 48 ) as $val )
{
?><option <?php value="<?php echo $val; ?> selected( get_query_var( 'woo_per_page' ), $val ); ?>>
<?php echo $val; ?>
</option><?php
}
?>
</select>
?>
}
Then we need a callback for pre_get_posts
:
add_action( 'pre_get_posts', 'WPSE116198PerPageCb' );
function WPSE116198PerPageCb( $query )
{
if (
isset( $_POST['woo_per_page'] )
AND in_array( absint( $_POST['woo_per_page'] ) ), array( 24, 36, 48 ) )
)
{
$per_page = absint( esc_attr( $_POST['woo_per_page'] ) );
$query->set( 'posts_per_page', $per_page );
}
return $query;
}
This code isn’t tested, I’m no WooCommerce expert and you’ll probably as well consider the offset
, the numposts
and paged
/page
for pagination.