Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
根据自定义字段重定向到Wordpress_Wordpress_Url - Fatal编程技术网

根据自定义字段重定向到Wordpress

根据自定义字段重定向到Wordpress,wordpress,url,Wordpress,Url,我的一些wordpress帖子有一个称为QR短码的自定义字段。我希望当有人访问domain.com/QRCODEVALUE时,能够重定向任何URL请求,并让他们使用相应的QR码直接发送到帖子。有没有办法做到这一点?我甚至不知道从哪里开始 domain.com/QRCODEVALUE是一个页面吗?如果是这样,您可以这样做: function my_redirect() { if ( is_page( 'QRCODEVALUE' ) ) { // Get the ID of

我的一些wordpress帖子有一个称为QR短码的自定义字段。我希望当有人访问domain.com/QRCODEVALUE时,能够重定向任何URL请求,并让他们使用相应的QR码直接发送到帖子。有没有办法做到这一点?我甚至不知道从哪里开始

domain.com/QRCODEVALUE是一个页面吗?如果是这样,您可以这样做:

function my_redirect() {
    if ( is_page( 'QRCODEVALUE' ) ) {

        // Get the ID of the post with the corresponding QR code here
        // Save the post ID to $post_id

        wp_redirect( get_permalink( $post_id ) ); 
        exit;
    }

}
add_action( 'template_redirect', 'my_redirect' );

我还没有测试过这一点,但是逻辑应该可以帮助你取得成功。抓取URL请求路径,删除尾部斜杠(如果存在),并查询自定义字段包含QRCODEVALUE的帖子:

<?php
function my_redirect() {
    $request = parse_url($_SERVER['REQUEST_URI']);
    $path = $request["path"];
    $path = explode('/', $path);
    $length = count($path);
    $result = $path[$length-1];
    $id;
    $query = new WP_Query( array('meta_key' => 'QRCODEVALUE', 'meta_value' => $result) );
    if($query->have_posts()): while($query->have_posts()): $query->the_post();
        $id = $post->ID;
    endwhile; endif;
    wp_redirect( get_permalink( $id ) ); 
    exit;
}
add_action('template_redirect', 'my_redirect');

否,domain.com/QRCODEVALUE正在重定向到自定义字段中包含QRCODEVALUE的页面。这有意义吗?我不确定我是否真的遵循了…?所以在我的600篇帖子中,每一篇都有一个称为QR码的字段,这是一个随机码。我希望人们能够通过在URL中输入自定义字段来访问该帖子。因此,如果一篇帖子的值为“dkh83d82d”,那么他们应该可以通过访问mydomain.com/dkh83d82dI查看它,因为他们也没有测试它,但上面的答案似乎是正确的。