【问题标题】:WordPress URL rewrite for WooCommerce attributesWooCommerce 属性的 WordPress URL 重写
【发布时间】:2014-08-02 22:29:27
【问题描述】:

我正在使用带有“YITH WooCommerce Ajax Navigation”插件的 WooCommerce 来过滤品牌。结果是一个显示为https://example.com/products/racquets/tennis-racquets/?filter_brands=47 的链接 理想情况下,我想改用https://example.com/products/racquets/tennis-racquets/brands/wilson

我尝试过使用 Apache mod_rewrite 规则,例如:

RewriteRule ^products/racquets/tennis-racquets/?filter_brands=47 /products/racquets/tennis-racquets/wilson [QSA,L]

我也尝试为我的functions.php 文件编写一个函数,但这似乎也没有成功。这是我尝试使用的代码示例。

function brand_rewrite_rules() {
    add_rewrite_rule( 'products/racquets/tennis-racquets/?filter_brands=47', 'products/racquets/tennis-racquets/wilson', 'top' );
    flush_rewrite_rules();
} 
add_action( 'init', 'brand_rewrite_rules' );

我确实尝试更新我的永久链接设置,但该功能没有做任何事情。任何人都可以为此提出解决方案吗?

【问题讨论】:

    标签: wordpress url mod-rewrite seo woocommerce


    【解决方案1】:

    这是一个添加Rewrite Endpoint的问题:

    添加端点会为提供的位掩码指定的每个匹配位置创建额外的重写规则。还将创建一个与端点同名的新查询变量。端点定义后面的字符串为此查询变量提供值(例如,"/foo/bar/" 变为 "?foo=bar")。

    <?php
    /**
     * Plugin Name: Add a Brand endpoint to the URLs
     * Plugin URI:  http://stackoverflow.com/a/24331768/1287812
     */
    
    add_action( 'init', function()
    {
        add_rewrite_endpoint( 'brands', EP_ALL );
    });
    
    add_filter( 'query_vars', function( $vars )
    {
        $vars[] = 'brands';
        return $vars;
    });
    
    /**
     * Refresh permalinks on plugin activation
     * Source: http://wordpress.stackexchange.com/a/108517/12615 
     */
    function WCM_Setup_Demo_on_activation()
    {
        if ( ! current_user_can( 'activate_plugins' ) )
            return;
    
        $plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : '';
        check_admin_referer( "activate-plugin_{$plugin}" );
    
        add_rewrite_endpoint( 'brands', EP_ALL ); #source: http://wordpress.stackexchange.com/a/118694/12615
        flush_rewrite_rules();
    }
    register_activation_hook(   __FILE__, 'WCM_Setup_Demo_on_activation' );
    

    然后,在模板中使用它:

    $brand = get_query_var('brand') ? urldecode( get_query_var('brand') ) : 'Empty endpoint';
    echo $brand;
    

    【讨论】:

      猜你喜欢
      • 2017-04-04
      • 1970-01-01
      • 2016-03-15
      • 1970-01-01
      • 2012-10-25
      • 2012-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多