Wordpress:如何在每篇文章中添加附加链接?
例如,我有这样的结构:Wordpress:如何在每篇文章中添加附加链接?,wordpress,Wordpress,例如,我有这样的结构: example.com/product/12345 产品-自定义帖子类型, 12345-后段塞。 我需要为每个帖子添加“/go”链接,所以 example.com/product/12345/go,将重定向到12345帖子元中的url。 我怎么能做到?(如果可能,无需附加插件)以下是解决方案。 functions.php: add_action('init', 'go_redirect'); function go_redirect() { add_rewrit
example.com/product/12345
产品-自定义帖子类型,12345-后段塞。
我需要为每个帖子添加“/go”链接,所以 example.com/product/12345/go,将重定向到12345帖子元中的url。
我怎么能做到?(如果可能,无需附加插件)以下是解决方案。
functions.php:
add_action('init', 'go_redirect');
function go_redirect() {
add_rewrite_rule( 'product\/([\d]*)\/go$', 'index.php?pagename=$matches[1]&redirect=1', 'top' );
add_filter( 'query_vars', function( $vars ){
$vars[] = 'redirect';
return $vars;
} );
}
在single.php的开头:
<?php
$go = get_query_var('redirect');
$post_id = $post->ID;
if ($go) {header('Location: ' . get_post_meta($post_id, 'redirect_url', true), true, 301);}
?>
可能重复您有一个元框字段,其中url是您希望链接example.com/product/12345/go重定向到的数据,对吗?Paul,对。