我正在寻找一种方法,使我的网站上的子公司链接的URL重定向。
类似于:site.com/go/keyword
我知道有很多插件可以做这个,但我不喜欢插件,我喜欢学习新东西。
我设想每个帖子有2个自定义字段,其中包括:
要重定向到的关键字url,但我不知道如何处理此重定向文件。
欢迎提供任何帮助或建议。
谢谢
我正在寻找一种方法,使我的网站上的子公司链接的URL重定向。
类似于:site.com/go/keyword
我知道有很多插件可以做这个,但我不喜欢插件,我喜欢学习新东西。
我设想每个帖子有2个自定义字段,其中包括:
要重定向到的关键字url,但我不知道如何处理此重定向文件。
欢迎提供任何帮助或建议。
谢谢
结合@alexander&;的answear;@dboris的
以下是最终解决方案:
创建页面以处理重定向添加到functions.php
add_action(\'init\', function() {
$page_id = 2100;
$page_data = get_post($page_id);
if(!is_object($page_data)) {
return;
}
add_rewrite_tag(\'%affiliate%\',\'([^/]+)\');
add_rewrite_rule(
\'go/([^/]+)/?$\',
\'index.php?pagename=\' . $page_data->post_name . \'&affiliate=$matches[1]\',
\'top\'
);
});
更换%affiliate%
和affiliate
按要使用的标记。代替page_id
使用步骤1中定义的页面ID。添加您分配给在sted 1上定义的页面的页面模型,并向其添加以下代码:
$args = array(
\'posts_per_page\' => -1,
\'meta_key\' => \'booking_slug\',
\'meta_value\' => $aff,
\'post_type\' => \'hotel\'
);
$query = new WP_Query( $cc_args );
if($query->have_posts()) : while ($query->have_posts() ) : $query->the_post();
the_title();
echo get_field(\'booking_affiliate\');
endwhile;
endif;
需要保护一点这些东西,并使用您想要的方式进行重定向!您需要使用add_rewrite_rule 注册新URL,然后创建新页面(&P);要处理链接位置的模板。
例如:
add_action(\'init\', function() {
$page_id = 2;
$page_data = get_post($page_id);
if(!is_object($page_data)) {
return;
}
add_rewrite_rule(
$page_data->post_name . \'/go/([^/]+)/?$\',
\'index.php?pagename=\' . $page_data->post_name . \'&affiliate=$matches[1]\',
\'top\'
);
});
更换$page_id
具有将处理链接的新页面的正确ID。您还需要使用add\\u filter来设置变量:
add_filter(\'query_vars\', function($vars) {
$vars[] = "affiliate";
return $vars;
});
&您的新页面;然后模板将使用访问此值get_query_var 并相应重定向:$affiliate = get_query_var(\'affiliate\', false);
// Do whatever lookup you need here to get the actual link to redirect to
wp_redirect($url);
exit;
更新:您还需要访问permalinks管理页面,以便应用新的重写规则。您还可以使用Rewrite Rules Inspector plugin, (虽然现在有点旧了)。此解决方案无需创建新页面或更改任何内容。
我们将:
1。设置新的重写规则并添加新的查询变量
2。在“pre\\u get\\u posts”钩子中捕获该情况,获取我们需要的帖子并执行重定向
请注意,如果您有多篇具有相同keyword_meta
值,这可能无法按计划工作。我尚未对此进行检查,将选择第一个匹配的帖子。
代码应进入functions.php
或类似:
add_action( \'init\',
function () {
add_rewrite_rule(
\'^(go)\\/([^\\/]+)$\',
\'index.php?redirection=$matches[2]\',
\'top\'
);
add_filter( \'query_vars\',
function ( $vars ) {
$vars[] = "redirection";
return $vars;
}
);
}
);
add_action( \'pre_get_posts\',
function ( $query ) {
if ( $redirection = get_query_var( \'redirection\' ) ) {
// Change this:
$keyword_meta = \'keyword_meta_field_name\';
$redirection_url_meta = \'redirection_url_meta_field_name\';
$post_type = \'post\';
$args = [
\'meta_key\' => $keyword_meta,
\'meta_value\' => $redirection,
\'posts_per_page\' => 1,
\'post_type\' => $post_type,
];
$post_query = new WP_Query( $args );
$posts = $post_query->get_posts();
// If no posts found, redirect back to referer
if ( count( $posts ) < 1 ) {
wp_safe_redirect( wp_get_referer() );
}
$redirection_url = get_post_meta( $posts[0]->ID, $redirection_url_meta, true );
// If no redirection URL found, redirect back to referer
if ( ! $redirection_url ) {
wp_safe_redirect( wp_get_referer() );
}
// Finally, do the redirection
if ( headers_sent() ) {
echo( "<script>location.href=\'$redirection_url\'</script>" );
} else {
header( "Location: $redirection_url" );
}
exit;
}
return $query;
}
);
Please do not forget to refresh your Permalinks in the dashboard (open Settings > Permalinks and click on Save Changes).
我已经尝试了这两种方法,但仍然收到错误消息:“抱歉,出于安全原因,不允许使用此文件类型。”// add support for webp mime types function webp_upload_mimes( $existing_mimes ) { // add webp to the list of mime types $existing_mimes[\'webp\'] = \'image/webp\'; // return th