Pagination 短码分页
我一直在想如何创建一个循环来支持woocommerce的分页短代码 因为目前,我有一个项目,希望有一个显示特定产品的页面,例如,我有50个销售产品,使用这个短代码[sale_products per_page=“10”],它只显示10个产品,没有分页。对这个问题有什么想法吗Pagination 短码分页,pagination,woocommerce,product,Pagination,Woocommerce,Product,我一直在想如何创建一个循环来支持woocommerce的分页短代码 因为目前,我有一个项目,希望有一个显示特定产品的页面,例如,我有50个销售产品,使用这个短代码[sale_products per_page=“10”],它只显示10个产品,没有分页。对这个问题有什么想法吗 关于,我采取了另一种方法。我所做的是创建一个“页面”,它将是一个产品档案(就像商店一样),除了销售产品。我敢肯定,如果不是快到午夜,我可以在WooCommerce中将其作为一个选项,就像设置店铺、我的帐户页面等的商店选项一样
关于,我采取了另一种方法。我所做的是创建一个“页面”,它将是一个产品档案(就像商店一样),除了销售产品。我敢肯定,如果不是快到午夜,我可以在WooCommerce中将其作为一个选项,就像设置店铺、我的帐户页面等的商店选项一样。但为了提高速度,我只是制作了一个页面,记录了它的ID,并将其硬编码为
$sale\page\ID
首先,我们将模仿WooCommerce的做法,当你告诉它你希望商店出现在你网站的首页上。基本上,我们将劫持查询并将其从页面切换到产品归档
add_action( 'pre_get_posts', 'so_28367762_pre_get_posts' );
function so_28367762_pre_get_posts( $q ) {
// We only want to affect the main query
if ( is_admin() || ! $q->is_main_query() ) {
return;
}
$sale_page_id = 487;
// totally modify the query on the Sale Page
if( isset( $q->queried_object_id ) && $q->queried_object_id === $sale_page_id ) {
$q->set( 'post_type', 'product' );
$q->set( 'page', '' );
$q->set( 'pagename', '' );
if ( isset( $q->query['paged'] ) ) {
$q->set( 'paged', $q->query['paged'] );
}
// Fix for verbose page rules
if ( $GLOBALS['wp_rewrite']->use_verbose_page_rules ) {
// Fix conditional Functions
$q->is_archive = true;
$q->is_post_type_archive = true;
$q->is_singular = false;
$q->is_page = false;
}
WC()->query->product_query( $q );
// We're on a shop page so queue the woocommerce_get_products_in_view function
add_action( 'wp', array( WC()->query, 'get_products_in_view' ), 2);
// And remove the pre_get_posts hook
WC()->query->remove_product_query();
}
}
然后我们将修改产品查询,使其只返回正在销售的商品。我试着直接在上面添加这个,但没有成功,因为product\u query()
方法中的post\u>从零开始/null。我添加了相同的条件逻辑,以确保这只应用于这个定制的“销售”页面
如果你有足够的销售产品,你应该有页码。今晚我唯一没想到的是,页面标题显示为“Shop”,而面包屑认为它是“Shop”。我相信有办法解决这个问题。。。调整模板等
add_action( 'pre_get_posts', 'so_28367762_pre_get_posts' );
function so_28367762_pre_get_posts( $q ) {
// We only want to affect the main query
if ( is_admin() || ! $q->is_main_query() ) {
return;
}
$sale_page_id = 487;
// totally modify the query on the Sale Page
if( isset( $q->queried_object_id ) && $q->queried_object_id === $sale_page_id ) {
$q->set( 'post_type', 'product' );
$q->set( 'page', '' );
$q->set( 'pagename', '' );
if ( isset( $q->query['paged'] ) ) {
$q->set( 'paged', $q->query['paged'] );
}
// Fix for verbose page rules
if ( $GLOBALS['wp_rewrite']->use_verbose_page_rules ) {
// Fix conditional Functions
$q->is_archive = true;
$q->is_post_type_archive = true;
$q->is_singular = false;
$q->is_page = false;
}
WC()->query->product_query( $q );
// We're on a shop page so queue the woocommerce_get_products_in_view function
add_action( 'wp', array( WC()->query, 'get_products_in_view' ), 2);
// And remove the pre_get_posts hook
WC()->query->remove_product_query();
}
}
小小的修改和工作为我 试试这个快捷码[sale\u products limit=“10”columns=“4”paginate=“true”]
从主题的函数.php
或自定义插件了解更多信息。不幸的是,我得到了:“错误404-找不到页面!”。我建议您保存永久链接,如果这不起作用,您需要创建自己的问题。不,这没有帮助。我已经有我自己的问题了:我将so_28367762_pre_get_posts()
函数的最后几行移到了“销售”页面上检查您的条件中。这似乎解决了“我的帐户”页面上的404错误问题。
add_action( 'pre_get_posts', 'so_28367762_pre_get_posts' );
function so_28367762_pre_get_posts( $q ) {
// We only want to affect the main query
if ( is_admin() || ! $q->is_main_query() ) {
return;
}
$sale_page_id = 487;
// totally modify the query on the Sale Page
if( isset( $q->queried_object_id ) && $q->queried_object_id === $sale_page_id ) {
$q->set( 'post_type', 'product' );
$q->set( 'page', '' );
$q->set( 'pagename', '' );
if ( isset( $q->query['paged'] ) ) {
$q->set( 'paged', $q->query['paged'] );
}
// Fix for verbose page rules
if ( $GLOBALS['wp_rewrite']->use_verbose_page_rules ) {
// Fix conditional Functions
$q->is_archive = true;
$q->is_post_type_archive = true;
$q->is_singular = false;
$q->is_page = false;
}
WC()->query->product_query( $q );
// We're on a shop page so queue the woocommerce_get_products_in_view function
add_action( 'wp', array( WC()->query, 'get_products_in_view' ), 2);
// And remove the pre_get_posts hook
WC()->query->remove_product_query();
}
}