我决定为正在销售的产品创建一个附加页面。为此,我复制了archive-product.php文件并创建了一个新模板。我已将此模板连接到我的新页面,并且遇到了不出现分页的问题。在Google中搜索了几个小时之后,我发现我的$ woocomerce_loop不允许我输入分页功能。我该如何解决?
我的销售页面代码
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
global $wp_query;
get_header( 'shop' );
$outside_opt = get_outside_opt();
$shoplayout = 'fullw';
if(isset($outside_opt['outside_shop_page_layout']) && $outside_opt['outside_shop_page_layout']!=''){
$shoplayout = $outside_opt['outside_shop_page_layout'];
}
$shopcolclass = 12;
switch($shoplayout) {
case 'fullw':
$shopcolclass = 12;
break;
default:
$shopcolclass = 9;
}
$wc_product_style = 1;
if(isset($outside_opt['product_style']) && $outside_opt['product_style'] !=''){
$wc_product_style = $outside_opt['product_style'];
}
?>
<!-- breadcrumb start -->
<?php get_template_part('/inc/bredcrumb/header-page-title'); ?>
<!-- breadcrumb end -->
<?php
/**
* woocommerce_before_main_content hook.
*
* @hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content)
* @hooked woocommerce_breadcrumb - 20
* @hooked WC_Structured_Data::generate_website_data() - 30
*/
remove_action( 'woocommerce_before_main_content' ,'woocommerce_breadcrumb',20 );
do_action( 'woocommerce_before_main_content' );
?>
<div class="page-section section pt-120 pb-120">
<div class="container">
<!--<div class="row">
<!-- Product Filter Toggle -->
<!--<div class="col-xs-12">
<button class="product-filter-toggle"><?php esc_html_e('filter','outside');?></button>
</div>
</div>-->
<!-- Product Filter Wrapper -->
<div class="row">
<div class="col-xs-12">
<?php get_sidebar('filter');?>
</div>
</div>
<div class="row">
<?php if( $shoplayout == 'left' ){ get_sidebar('category'); } ?>
<div class="col-xs-12 <?php echo 'col-md-'.$shopcolclass; ?>">
<div class="row">
<div class="col-md-12">
<?php
/**
* woocommerce_before_shop_loop hook.
*
* @hooked wc_print_notices - 10
* @hooked woocommerce_result_count - 20
* @hooked woocommerce_catalog_ordering - 30
*/
remove_action( 'woocommerce_before_shop_loop' ,'woocommerce_result_count',20 );
remove_action( 'woocommerce_before_shop_loop' ,'woocommerce_catalog_ordering',30 );
do_action( 'woocommerce_before_shop_loop' );
?>
</div>
</div>
<div class="row">
<?php $args = array(
'post_type' => 'product',
'posts_per_page' => 12,
'meta_query' => array(
'relation' => 'OR',
array( // Simple products type
'key' => '_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
),
)
);
$wp_query = new WP_Query( $args );
if ( have_posts() ) : ?>
<?php woocommerce_product_loop_start(); ?>
<?php woocommerce_product_subcategories(); ?>
<?php if ($wc_product_style == 2):?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
/**
* woocommerce_shop_loop hook.
*
* @hooked WC_Structured_Data::generate_product_data() - 10
*/
do_action( 'woocommerce_shop_loop' );
?>
<?php wc_get_template('inc/wooproduct/productstyletwo.php');?>
<?php endwhile; // end of the loop. ?>
<?php else:?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
/**
* woocommerce_shop_loop hook.
*
* @hooked WC_Structured_Data::generate_product_data() - 10
*/
do_action( 'woocommerce_shop_loop' );
?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php endif;?>
<?php print_r ($GLOBALS['woocommerce_loop']); woocommerce_product_loop_end(); ?>
<?php
/**
* woocommerce_after_shop_loop hook.
*
* @hooked woocommerce_pagination - 10
*/
remove_action( 'woocommerce_after_shop_loop' ,'woocommerce_pagination',10 );
do_action( 'woocommerce_after_shop_loop' );
?>
<?php elseif ( ! woocommerce_product_subcategories( array( 'before' => woocommerce_product_loop_start( false ), 'after' => woocommerce_product_loop_end( false ) ) ) ) : ?>
<?php
/**
* woocommerce_no_products_found hook.
*
* @hooked wc_no_products_found - 10
*/
do_action( 'woocommerce_no_products_found' );
?>
<?php endif; ?>
</div>
<div class="row">
<div class="col-md-12 text-center"><?php
woocommerce_pagination();?></div>
</div>
</div>
<?php if( $shoplayout == 'right' ){ get_sidebar('category'); } ?>
</div>
</div>
</div>
<?php
/**
* woocommerce_after_main_content hook.
*
* @hooked woocommerce_output_content_wrapper_end - 10 (outputs closing divs for the content)
*/
do_action( 'woocommerce_after_main_content' );
?>
<?php
/**
* woocommerce_sidebar hook.
*
* @hooked woocommerce_get_sidebar - 10
*/
//do_action( 'woocommerce_sidebar' );
?>
<?php get_footer( 'shop' ); ?>
当我尝试在woocommerce_pagination()之前调用print_r以获得$ woocomerce_loop时; 我得到了下一个值: [循环] => 0 [列] => 4 [名称] => [is_shortcode] => [is_paginated] => 1 [is_search] => [is_filtered] => [total] => 0 [total_pages] => 0 [每页] => 0 [当前页] => 1
这也是我实际使用的archive-product.php模板:
<?php
/**
* The Template for displaying product archives, including the main shop page which is a post type archive
*
* This template can be overridden by copying it to yourtheme/woocommerce/archive-product.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* @see https://docs.woocommerce.com/document/template-structure/
* @author WooThemes
* @package WooCommerce/Templates
* @version 3.4.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
get_header( 'shop' );
$outside_opt = get_outside_opt();
$shoplayout = 'fullw';
if(isset($outside_opt['outside_shop_page_layout']) && $outside_opt['outside_shop_page_layout']!=''){
$shoplayout = $outside_opt['outside_shop_page_layout'];
}
$shopcolclass = 12;
switch($shoplayout) {
case 'fullw':
$shopcolclass = 12;
break;
default:
$shopcolclass = 9;
}
$wc_product_style = 1;
if(isset($outside_opt['product_style']) && $outside_opt['product_style'] !=''){
$wc_product_style = $outside_opt['product_style'];
}
?>
<!-- breadcrumb start -->
<?php get_template_part('/inc/bredcrumb/header-page-title'); ?>
<!-- breadcrumb end -->
<?php
/**
* woocommerce_before_main_content hook.
*
* @hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content)
* @hooked woocommerce_breadcrumb - 20
* @hooked WC_Structured_Data::generate_website_data() - 30
*/
remove_action( 'woocommerce_before_main_content' ,'woocommerce_breadcrumb',20 );
do_action( 'woocommerce_before_main_content' );
?>
<div class="page-section section pt-120 pb-120">
<div class="container">
<!--<div class="row">
<!-- Product Filter Toggle -->
<!--<div class="col-xs-12">
<button class="product-filter-toggle"><?php esc_html_e('filter','outside');?></button>
</div>
</div>-->
<!-- Product Filter Wrapper -->
<div class="row">
<div class="col-xs-12">
<?php get_sidebar('filter');?>
</div>
</div>
<div class="row">
<?php if( $shoplayout == 'left' ){ get_sidebar('category'); } ?>
<div class="col-xs-12 <?php echo 'col-md-'.$shopcolclass; ?>">
<div class="row">
<div class="col-md-12">
<?php
/**
* woocommerce_before_shop_loop hook.
*
* @hooked wc_print_notices - 10
* @hooked woocommerce_result_count - 20
* @hooked woocommerce_catalog_ordering - 30
*/
remove_action( 'woocommerce_before_shop_loop' ,'woocommerce_result_count',20 );
remove_action( 'woocommerce_before_shop_loop' ,'woocommerce_catalog_ordering',30 );
do_action( 'woocommerce_before_shop_loop' );
?>
</div>
</div>
<div class="row">
<?php if ( have_posts() ) : ?>
<?php woocommerce_product_loop_start(); ?>
<?php woocommerce_product_subcategories(); ?>
<?php if ($wc_product_style == 2):?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
/**
* woocommerce_shop_loop hook.
*
* @hooked WC_Structured_Data::generate_product_data() - 10
*/
do_action( 'woocommerce_shop_loop' );
?>
<?php wc_get_template('inc/wooproduct/productstyletwo.php');?>
<?php endwhile; // end of the loop. ?>
<?php else:?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
/**
* woocommerce_shop_loop hook.
*
* @hooked WC_Structured_Data::generate_product_data() - 10
*/
do_action( 'woocommerce_shop_loop' );
?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php endif;?>
<?php woocommerce_product_loop_end(); ?>
<?php
/**
* woocommerce_after_shop_loop hook.
*
* @hooked woocommerce_pagination - 10
*/
remove_action( 'woocommerce_after_shop_loop' ,'woocommerce_pagination',10 );
do_action( 'woocommerce_after_shop_loop' );
?>
<?php elseif ( ! woocommerce_product_subcategories( array( 'before' => woocommerce_product_loop_start( false ), 'after' => woocommerce_product_loop_end( false ) ) ) ) : ?>
<?php
/**
* woocommerce_no_products_found hook.
*
* @hooked wc_no_products_found - 10
*/
do_action( 'woocommerce_no_products_found' );
?>
<?php endif; ?>
</div>
<div class="row">
<div class="col-md-12 text-center"><?php woocommerce_pagination();?></div>
</div>
</div>
<?php if( $shoplayout == 'right' ){ get_sidebar('category'); } ?>
</div>
</div>
</div>
<?php
/**
* woocommerce_after_main_content hook.
*
* @hooked woocommerce_output_content_wrapper_end - 10 (outputs closing divs for the content)
*/
do_action( 'woocommerce_after_main_content' );
?>
<?php
/**
* woocommerce_sidebar hook.
*
* @hooked woocommerce_get_sidebar - 10
*/
//do_action( 'woocommerce_sidebar' );
?>
<?php get_footer( 'shop' ); ?>