WooCommerce 属性的 WordPress URL 重写

编程入门 行业动态 更新时间:2024-10-09 22:15:43
本文介绍了WooCommerce 属性的 WordPress URL 重写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用 WooCommerce 和YITH WooCommerce Ajax 导航"插件来过滤品牌.结果是显示为 example/products/racquets/tennis-racquets/?filter_brands=47 的链接理想情况下,我想改用 example/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 example/products/racquets/tennis-racquets/?filter_brands=47 Ideally, I would like to use example/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?

推荐答案

这是添加一个重写端点的问题:

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

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: stackoverflow/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: wordpress.stackexchange/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: wordpress.stackexchange/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 重写

本文发布于:2023-11-01 11:13:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1549185.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重写   属性   WooCommerce   URL   WordPress

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!