plugins - Dropdown menu on custom page with product to choose number of products per page

I have almost zero knowledge with Wordpress and Wocoommerce cores and api's so, please bear with me. I have showing

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.

Share Improve this question edited Mar 25, 2019 at 13:17 S.I. asked Mar 25, 2019 at 12:23 S.I.S.I. 1251 silver badge8 bronze badges 3
  • First thing I noticed is that there appears to be a typo on row 3 for $per_page as the second parameter for filter_inputis perpagee and on row 14 it's just perpage. – 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
Add a comment  | 

1 Answer 1

Reset to default 1

Maybe 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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信