WordPress嵌套重写端点

时间:2017-03-19 13:25:39

标签: php wordpress

我正在开发一个WordPress电子商务插件。 我使用自定义数据库表来存储产品,因此要在前端访问它们,我需要使用自定义端点。 添加简单的重写端点工作正常,但我想知道如何添加嵌套的重写端点。

function mycustom_add_endpoint() {
    add_rewrite_endpoint( 'my-products', EP_ROOT );
}
add_action( 'init', 'mycustom_add_endpoint' );


function mycustom_endpoints_activate() {
    // ensure our endpoint is added before flushing rewrite rules
    mycustom_add_endpoint();
    // flush rewrite rules - only do this on activation as anything more frequent is bad!
    flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'mycustom_endpoints_activate' );

function mycustom_template_redirect() {
    global $wp_query;

    // if this is not a request for my-products or a singular object then bail
    if ( ! isset( $wp_query->query_vars['my-products'] ))
        return;

    // include custom template
    include dirname( __FILE__ ) . '/template.php';
    exit;
}
add_action( 'template_redirect', 'mycustom_template_redirect' );

上面的代码添加了http://example.com/my-products个端点,因此我可以访问单个产品http://example.com/my-products/product-name

现在我需要以某种方式构建一个逻辑,通过http://example.com/my-products/tags/tag-name URL访问产品标签。 提前致谢。 最诚挚的问候, 太

0 个答案:

没有答案