简体   繁体   English

WooCommerce Shop Page Endpoint。 没有产品

[英]WooCommerce Shop Page Endpoint. No Products

I've been trying to add an endpoint for the shop page using this code: 我一直在尝试使用以下代码为商店页面添加端点:

add_action( 'init', 'add_city_endpoint' );
function add_city_endpoint() {
    add_rewrite_endpoint( 'city', EP_PAGES );
}

to get products filtered when url is something like: /shop/city/new-york/ 当url类似于: / shop / city / new-york /

But I get page with no products. 但我得到没有产品的页面。

How can I make it work? 我怎样才能使它工作?

Alright, I'll answer my own question. 好吧,我会回答我自己的问题。

Since the shop page is not an actual wordpress page, but an archive page for "product" post type, the endpoints don't work well for my purpose. 由于商店页面不是实际的wordpress页面,而是“产品”帖子类型的存档页面,因此端点不能很好地用于我的目的。 And the proper way would be to use add_rewrite_rule instead: 正确的方法是使用add_rewrite_rule:

add_action( 'init', 'city_add_rewrite_rules' );
function city_add_rewrite_rules() {

    $shop_page = ( $shop_page_id = wc_get_page_id( 'shop' ) ) && get_post( $shop_page_id ) ? get_page_uri( $shop_page_id ) : 'shop';

    add_rewrite_tag( '%sr_city%', '([^&]+)' );
    add_rewrite_rule(
            "^$shop_page/city/([^/]+)/?$", 'index.php?post_type=product&sr_city=$matches[1]', 'top'
    );
    //pagination
    add_rewrite_rule(
            "^$shop_page/city/([^/]+)/page/([0-9]+)?$", 'index.php?post_type=product&sr_city=$matches[1]&paged=$matches[2]', 'top'
    );

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM