2017-03-07 31 views
0

例如,我有这样的结构:WordPress的:如何使每个职位的额外链接?

example.com/product/12345 

产物 - 自定义后型,
12345 - 交蛞蝓。
我需要为每个帖子制作额外的“/ go”链接,因此 example.com/product/12345/go,将重定向到将在12345帖子的met​​a中的url。
我该如何做到这一点? (如果可能,不需要额外的插件)

+1

http://stackoverflow.com/questions/38058749/multiple-urls-for-single-custom-post -type-in​​-wordpress – ashanrupasinghe

+0

可能的重复[多个URL为单个自定义帖子类型在WordPress](http://stackoverflow.com/questions/38058749/multiple-urls-for-single-custom-post-type-in​​- wordpress) – yivi

+0

你已经有了一个meta box字段,其中包含一个url作为你想要链接example.com/product/12345/go重定向到的数据,这是否正确? – Paul

回答

0

下面是解决方案。
的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);} 
?> 
相关问题