admin管理员组文章数量:1125559
I am redirecting a specific url format using add_rewrite_rule
after adding an additional query_var
(mlang) to it.
Example:
http://localhost/periodicals/sri-bn
is to be redirected tohttp://localhost/index.php?name=sri&mlang=bn
(
periodicals
is declared as a custom post type)
The redirection is working fine except I am missing the query_var
value in the theme/template page for the post type 'periodicals' (single-periodicals.php).
Here is my redirection code:
class PxMultiLang{
public function __construct()
{
define('PX_MULTI_LANG', [
['code'=>'', 'lang'=> 'English'],
['code'=>'bn', 'lang'=> 'Bengali'],
['code'=>'hn', 'lang'=> 'Hindi']
]);
add_filter('query_vars', [$this, 'px_multilang_query_var']);
add_action('init', [$this, 'px_multilang_multi_redirect']);
}
function px_multilang_multi_redirect()
{
add_rewrite_tag('%mlang%', '([^/]+)');
foreach(PX_MULTI_LANG as $tlk => $tlv)
{
if($tlk > 0)
{
add_rewrite_rule(
'^periodicals/([^/]+)-'.$tlv['code'].'[/]?$',
'index.php?name=$matches[1]&mlang='.$tlv['code'],
'top'
);
}
}
}
function px_multilang_query_var($query_vars)
{
$query_vars[]= 'mlang';
return $query_vars;
}
}
In the theme file (single-periodicals.php), I have tried to capture the mlang query_var
without success. The code I used is as:
$multilang= isset($_REQUEST['mlang'])? $_REQUEST['mlang']:'';
$multilang2= get_query_var('mlang');
global $wp_query;
$multilang3= $wp_query->query_vars['mlang'];
echo '$multilang= '.$multilang; //empty value
echo ' / $multilang 2= '.$multilang2; //empty value
echo ' / $multilang 3= '.$multilang3; //empty value
echo '<pre>'; print_r($wp_query->query_vars); echo '</pre>'; //No trace of "mlang" in the output
Please advice. Thank you for reading this far.
I am redirecting a specific url format using add_rewrite_rule
after adding an additional query_var
(mlang) to it.
Example:
http://localhost/periodicals/sri-bn
is to be redirected tohttp://localhost/index.php?name=sri&mlang=bn
(
periodicals
is declared as a custom post type)
The redirection is working fine except I am missing the query_var
value in the theme/template page for the post type 'periodicals' (single-periodicals.php).
Here is my redirection code:
class PxMultiLang{
public function __construct()
{
define('PX_MULTI_LANG', [
['code'=>'', 'lang'=> 'English'],
['code'=>'bn', 'lang'=> 'Bengali'],
['code'=>'hn', 'lang'=> 'Hindi']
]);
add_filter('query_vars', [$this, 'px_multilang_query_var']);
add_action('init', [$this, 'px_multilang_multi_redirect']);
}
function px_multilang_multi_redirect()
{
add_rewrite_tag('%mlang%', '([^/]+)');
foreach(PX_MULTI_LANG as $tlk => $tlv)
{
if($tlk > 0)
{
add_rewrite_rule(
'^periodicals/([^/]+)-'.$tlv['code'].'[/]?$',
'index.php?name=$matches[1]&mlang='.$tlv['code'],
'top'
);
}
}
}
function px_multilang_query_var($query_vars)
{
$query_vars[]= 'mlang';
return $query_vars;
}
}
In the theme file (single-periodicals.php), I have tried to capture the mlang query_var
without success. The code I used is as:
$multilang= isset($_REQUEST['mlang'])? $_REQUEST['mlang']:'';
$multilang2= get_query_var('mlang');
global $wp_query;
$multilang3= $wp_query->query_vars['mlang'];
echo '$multilang= '.$multilang; //empty value
echo ' / $multilang 2= '.$multilang2; //empty value
echo ' / $multilang 3= '.$multilang3; //empty value
echo '<pre>'; print_r($wp_query->query_vars); echo '</pre>'; //No trace of "mlang" in the output
Please advice. Thank you for reading this far.
Share Improve this question asked Feb 8, 2024 at 20:44 sariDonsariDon 2651 gold badge2 silver badges18 bronze badges1 Answer
Reset to default 0Changing the add_rewrite_rule
did the trick:
add_rewrite_rule('^periodicals/([^/]+)-'.$tlv['code'].'[/]?$', 'index.php?periodicals=$matches[1]&post_type=periodicals&name=$matches[1]&mlang='.$tlv['code'], 'top');
So, I assume for custom post types, using add_rewrite_rule
with index.php as query need some extra query variables. A sample will be as:
add_rewrite_rule('^<custom_post_type>/([^/]+)-'.$tlv['code'].'[/]?$', 'index.php?<custom_post_type>=$matches[1]&post_type=<custom_post_type>&name=$matches[1]&mlang='.$tlv['code'], 'top');
本文标签: url rewritingqueryvar values empty in theme file after addrewriterule redirection
版权声明:本文标题:url rewriting - `query_var` values empty in theme file after `add_rewrite_rule` redirection 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736657981a1946304.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论