【问题标题】:Wordpress: Access to post from wp_headersWordpress:从 wp_headers 访问帖子
【发布时间】:2017-05-04 07:16:49
【问题描述】:

我正在尝试为我的 wordpress 安装设置一个新的 http 标头,但我无法在我的新 wp_headers 过滤器函数中使用 $post 对象。我想为不同的帖子类型发送不同的标题,并使用 Go(lang) 来缓存东西(家庭项目)。

    function add_new_header($headers) { 
        $headers['PostId'] = get_the_ID();
        return $headers;
    }

    add_filter('wp_headers', 'add_new_header');

【问题讨论】:

    标签: php wordpress http-headers


    【解决方案1】:

    似乎我无法访问挂钩中的 Post / get_queried_object_id(),因为它没有启动。

    所以,引用帖子属性,你必须在“template_redirect”钩子中做。就像在那一刻帖子存在......

        add_action('template_redirect', 'add_new_header');
        function add_new_header($headers) { 
            $post_id = get_queried_object_id();
            if( $post_id ) {
                header("PostId: " .  $post_id) ;
            }
        }
    

    希望对某人有所帮助……总有一天……

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-27
      • 1970-01-01
      • 1970-01-01
      • 2020-03-16
      • 2015-09-20
      • 1970-01-01
      • 2015-10-14
      • 1970-01-01
      相关资源
      最近更新 更多