admin管理员组文章数量:1026989
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).
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.
-
First thing I noticed is that there appears to be a typo on row 3 for
$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 - @AnttiKoskinen it's fixed. Just copied wrong snipped. Edited in question too. – S.I. Commented Mar 25, 2019 at 13:17
- I updated the code example in my answer, if you want to give it a try. – Antti Koskinen Commented Mar 25, 2019 at 14:06
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).
本文标签: pluginsDropdown menu on custom page with product to choose number of products per page
版权声明:本文标题:plugins - Dropdown menu on custom page with product to choose number of products per page 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745660724a2161873.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