Skip to content

Instantly share code, notes, and snippets.

@pavlo-bondarchuk
Created August 12, 2021 15:00
Show Gist options
  • Save pavlo-bondarchuk/72f742afa5a6af99597f4e5f942d6d05 to your computer and use it in GitHub Desktop.
Save pavlo-bondarchuk/72f742afa5a6af99597f4e5f942d6d05 to your computer and use it in GitHub Desktop.
add a products per-page select dropdown to archive - above shop productloop
// add a products per-page select dropdown to archive - above shop productloop
add_action( 'woocommerce_before_shop_loop', 'pro_selectbox', 30 );
function pro_selectbox() {
$per_page = filter_input(INPUT_GET, 'perpage', FILTER_SANITIZE_NUMBER_INT);
$orderby_options = array(
'16' => '16',
'32' => '32',
'64' => '64',
'96' => '96'
);
echo '<div class="woocommerce-perpage">';
echo '<select onchange="if (this.value) window.location.href=this.value">';
foreach( $orderby_options as $value => $label ) {
echo "<option " . selected( $per_page, $value ) . " value='?perpage=$value'>$label</option>";
}
echo '</select>';
echo '</div>';
}
add_action( 'pre_get_posts', 'pro_pre_get_products_query', 9999999999999 );
function pro_pre_get_products_query( $query ) {
$per_page = filter_input(INPUT_GET, 'perpage', FILTER_SANITIZE_NUMBER_INT);
if( $query->is_main_query() && !is_admin() ) {
$query->set( 'posts_per_page', $per_page );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment