gpt4 book ai didi

WooCommerce 属性的 WordPress URL 重写

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:31:28 25 4
gpt4 key购买 nike

我使用带有“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' );

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

最佳答案

就是加一个Rewrite Endpoint的问题:

Adding an endpoint creates extra rewrite rules for each of the matching places specified by the provided bitmask. A new query var with the same name as the endpoint will also be created. The string following the endpoint definition supplies the value for this query var (e.g. "/foo/bar/" becomes "?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;

关于WooCommerce 属性的 WordPress URL 重写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24187090/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com