I have almost zero knowledge with Wordpress and Wocoommerce cores and api's so, please bear with me.
I have showing ~140 products on custom page (e.g. not the default shop page for woocommerce) trough WooCommerce Product Filter
.
The products are on page and everything is fine except there is no way to show dropdown Products per page
which I can choose how many products to show on page.
So, I've found some tutorials and currently I have this in my function.php
add_action( 'woocommerce_before_shop_loop', 'ps_selectbox', 25 );
function ps_selectbox() {
$per_page = filter_input(INPUT_GET, 'perpage', FILTER_SANITIZE_NUMBER_INT);
echo '<span>Per Page: </span>';
echo '<select onchange="if (this.value) window.location.href=this.value">';
$orderby_options = array( '8' => '8', '16' => '16','32' => '32','64' => '64' );
foreach( $orderby_options as $value => $label ) {
echo "<option ".selected( $per_page, $value )." value='?perpage=$value'>$label</option>";
}
echo '</select>';
}
add_action( 'pre_get_posts', 'ps_pre_get_products_query' );
function ps_pre_get_products_query( $query ) {
$per_page = filter_input(INPUT_GET, 'perpage', FILTER_SANITIZE_NUMBER_INT);
if( $query->is_main_query() && !is_admin() && is_page() ) {
$query->set( 'posts_per_page', $per_page );
}
}
function render_ps_sel() {
return ps_selectbox();
}
add_shortcode( 'ps_selectbox', 'render_ps_sel' );
I'm render the dropdown menu trough the shortcode above [ps_selectbox]
but whatever I choose the number of the products on the page is always same and nothing change.
I have almost zero knowledge with Wordpress and Wocoommerce cores and api's so, please bear with me.
I have showing ~140 products on custom page (e.g. not the default shop page for woocommerce) trough WooCommerce Product Filter
.
The products are on page and everything is fine except there is no way to show dropdown Products per page
which I can choose how many products to show on page.
So, I've found some tutorials and currently I have this in my function.php
add_action( 'woocommerce_before_shop_loop', 'ps_selectbox', 25 );
function ps_selectbox() {
$per_page = filter_input(INPUT_GET, 'perpage', FILTER_SANITIZE_NUMBER_INT);
echo '<span>Per Page: </span>';
echo '<select onchange="if (this.value) window.location.href=this.value">';
$orderby_options = array( '8' => '8', '16' => '16','32' => '32','64' => '64' );
foreach( $orderby_options as $value => $label ) {
echo "<option ".selected( $per_page, $value )." value='?perpage=$value'>$label</option>";
}
echo '</select>';
}
add_action( 'pre_get_posts', 'ps_pre_get_products_query' );
function ps_pre_get_products_query( $query ) {
$per_page = filter_input(INPUT_GET, 'perpage', FILTER_SANITIZE_NUMBER_INT);
if( $query->is_main_query() && !is_admin() && is_page() ) {
$query->set( 'posts_per_page', $per_page );
}
}
function render_ps_sel() {
return ps_selectbox();
}
add_shortcode( 'ps_selectbox', 'render_ps_sel' );
I'm render the dropdown menu trough the shortcode above [ps_selectbox]
but whatever I choose the number of the products on the page is always same and nothing change.
1 Answer
Reset to default 1Maybe this would work?
function ps_pre_get_products_query( $query ) {
$per_page = filter_input(INPUT_GET, 'perpage', FILTER_SANITIZE_NUMBER_INT);
if ( ! is_admin() && is_woocommerce() && is_page() ) {
$query->set( 'posts_per_page', $per_page );
}
}
I tested your code and the if statement didn't seem to work with is_page()
(but that could also be because my local WP sandbox is a mess). To my knowing posts_per_page
expects int
and var_dump
showed that $per_page
was a string.
EDIT My local WP sandbox is a mess, so that's why is_page
wasn't working. I tested the code again on another install.
EDIT 2 Let's see how many times I can get this wrong. I updated the code to something that works (at least on another local WP sandbox).
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745660725a4638819.html
$per_page
as the second parameter forfilter_input
isperpagee
and on row 14 it's justperpage
. – Antti Koskinen Commented Mar 25, 2019 at 13:14