本文介绍了WooCommerce属性的WordPress URL重写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将WooCommerce与"YITH WooCommerce Ajax导航"插件一起使用来过滤品牌.结果是一个链接显示为https://example.com/products/racquets/tennis-racquets/?filter_brands=47理想情况下,我想改用https://example.com/products/racquets/tennis-racquets/brands/wilson.

I'm using WooCommerce with the "YITH WooCommerce Ajax Navigation" plugin to filter brands. The result is a link that appears as https://example.com/products/racquets/tennis-racquets/?filter_brands=47Ideally, I would like to use https://example.com/products/racquets/tennis-racquets/brands/wilson instead.

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

I've tried using an Apache mod_rewrite rule such as:

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

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

I've also tried writing a function for my functions.php file but that doesn't seem to catch either. Here's a sample of the code I tried using.

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' );

我确实尝试更新我的永久链接设置,但是该功能没有执行任何操作.有人可以为此提出解决方案吗?

I did try updating my permalink settings but the function did not do anything. Can anyone propose a solution for this?

推荐答案

只需添加重写端点即可:

<?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' );

然后,在模板中按如下方式使用它:

Then, in the templates use it like:

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

这篇关于WooCommerce属性的WordPress URL重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 08:05