0

Есть такая структура:

example.com/product/12345

product - custom post-type,
12345 - post slug.
Мне нужно для каждого поста сделать дополнительный /go линк (example.com/product/12345/go) при переходе на который будет редиректить на урл взятый из мета-данных у этого же поста.
Как такое можно реализовать?

1 ответ 1

1

Разобрался.
В 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);}
?>

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.