how to retrieve custom field at root directory for wordpress?

i have create a redir.php and placed in the home directory of wordpress to do the redirect. I want to pass in the post id and then retrieve a custom field value of the post and feed into header('location:'.$url);

www.mysite.com/redir.php?id=30 in redir.php will retrieve post id=30 custom field value and pass it into $url.

this is what I have, but it's not working. It gives me "Parse error: parse error in redir.php on line 5".

it looks like wordpress environment is not being loaded.

 <?php

    require('./wp-blog-header.php');
      $id = $_GET['id'];
  $url= get_field('deal_http_link',post->$id);
    header('Location:'.$url);
?>

thanks


Your script has multiple issues:

  • There is whitespace before the opening <?php tag, so the redirect wouldn't work because the headers will have already been sent. Instead, <?php should be the very first thing in the file.
  • post->$id is invalid syntax. You probably meant the $id variable which you defined in the preceding line.
  • To retrieve the value of a custom field, use get_post_meta(), not get_field().
  • Try something like this instead:

    <?php
    require('./wp-blog-header.php');
    $id = $_GET['id'];
    $url = get_post_meta($id, 'deal_http_link', true);
    header('Location: ' . $url);
    exit();
    
    链接地址: http://www.djcxy.com/p/34742.html

    上一篇: 如何使用链接而不是警报?

    下一篇: 如何检索wordpress根目录下的自定义字段?