Created
May 16, 2025 05:42
-
-
Save hssktm/50f53e9daf6d3ada51a85d42b18c521c to your computer and use it in GitHub Desktop.
Per page Woocommerce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_filter('woocommerce_get_settings_products', 'add_products_per_page_setting', 10, 2); | |
function add_products_per_page_setting($settings, $current_section) { | |
if ('' === $current_section) { | |
$new = array(); | |
foreach ($settings as $setting) { | |
$new[] = $setting; | |
if (isset($setting['id']) && 'woocommerce_shop_page_id' === $setting['id']) { | |
$new[] = array( | |
'title' => __('Products per page', 'woocommerce'), | |
'desc' => __('Number of products to display per page on shop archives.', 'woocommerce'), | |
'id' => 'woocommerce_products_per_page', | |
'default' => '12', | |
'type' => 'number', | |
'css' => 'min-width:150px;', | |
'desc_tip' => true, | |
); | |
} | |
} | |
return $new; | |
} | |
return $settings; | |
} | |
add_action('pre_get_posts', 'custom_products_per_page'); | |
function custom_products_per_page($query) { | |
if (! is_admin() && $query->is_main_query() && (is_shop() || is_post_type_archive('product') || is_product_taxonomy())) { | |
$per_page = get_option('woocommerce_products_per_page', 12); | |
$query->set('posts_per_page', intval($per_page)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment