我想在分页后在我的woo commerce产品页面上添加“查看全部”按钮。单击“查看全部”按钮后,将打开所有产品。
在WooCommerce产品档案页面上添加“全部查看”按钮
3 个回复
SO网友:djboris
我将为您提供一种不同的方法,无需实际编辑模板文件。您只需将这两个动作挂钩添加到function.php
文件(确保使用child theme):
/**
* This will add a \'Show All\' link after the pagination on the shop pages.
* It will be hidden once it is activated.
*/
add_action( \'woocommerce_after_shop_loop\', \'wpse333192_add_showall\', 40 );
function wpse333192_add_showall() {
if ( ! isset( $_GET[\'showall\'] ) ) {
global $wp;
echo sprintf(
"<a href=\'%s\'>%s</a>",
home_url( add_query_arg( array_merge( $_GET, [ \'showall\' => 1 ] ), $wp->request ) ),
__( \'Show All\', \'text-domain\' )
);
}
}
/**
* This will alter the main product query if \'showall\' is activated
*/
add_action( \'pre_get_posts\', \'wpse333192_alter_query_showall\' );
function wpse333192_alter_query_showall( $query ) {
/**
* Alter the query only if it is:
* 1. The main query
* 2. Post type is product
* 3. $_GET[\'showall\'] is set
* 4. $_GET[\'showall\'] equals 1
*/
if ( $query->is_main_query()
&& $query->get( \'post_type\' ) == \'product\'
&& isset( $_GET[\'showall\'] )
&& $_GET[\'showall\'] == 1
) {
// Load the \'first\' page
$query->set( \'paged\', 1 );
// Set post per page to unlimited
$query->set( \'posts_per_page\', - 1 );
}
return $query;
}
SO网友:Tanmay Patel
Put below code after 此操作<?php do_action(\'woocommerce_after_shop_loop\' ); ?>
在里面archive-product.php
文件
For Category Products Page
<?php
$cat_data = get_queried_object();
$cat_id = $cat_data->term_id;
if(is_product_category() && !isset($_GET[\'showall\'])){ ?>
<a href="<?php echo get_term_link( $cat_id, \'product_cat\' ) . "?showall=1"; ?>">Show all</a>
<?php } ?>
For Shop Page
<?php if(is_shop() && !isset($_GET[\'showall\'])){ ?>
<a href="<?php echo get_permalink( woocommerce_get_page_id( \'shop\' ) ) . "?showall=1"; ?>">Show all</a>
<?php } ?>
只需将条件检查添加到functions.php
文件if(isset( $_GET[\'showall\']) ){
add_filter( \'loop_shop_per_page\', create_function( \'$cols\', \'return -1;\' ) );
} else {
$default_posts_per_page = get_option( \'posts_per_page\' );
add_filter( \'loop_shop_per_page\', create_function( \'$cols\', \'return \'.$default_posts_per_page.\';\' ) );
}
SO网友:g10der
create\\u function()已弃用。您应该使用以下代码来获得所需的结果。
add_filter( \'loop_shop_per_page\', \'show_all\' );
function show_all($cols){
if(isset( $_GET[\'showall\']) ){
return $cols = -1;
} else {
$default_posts_per_page = get_option( \'posts_per_page\' );
return $default_posts_per_page;
}
}