我一直在寻找一段时间但没有确定的答案。我希望将文本附加到WordPress网站,以获取从一个域移动到新域并保留内容的先前帖子。
所以我想要做的是,添加“这篇文章最初发布于xyz.com。”在今天的日期之前发布的所有帖子。
现在可以通过数据库或WP函数过滤器来完成,只要它持久,我就可以使用任何一个选项。
有关如何解决这个问题的任何建议都会受到赞赏吗?
答案 0 :(得分:1)
您可以使用app.use((req, res, next) => {
req.hostname = req.headers['x-forwarded-host'] || req.hostname;
req.test = req.headers['x-forwarded-proto'] || req.protocol;
req.protocol = req.headers['x-forwarded-proto'] || req.protocol;
console.log(req.hostname);
console.log(req.test);
console.log(req.protocol);
next();
})
这样的过滤器:
req.hostname = '127.0.0.1'
req.test = 'https'
req.protocol = 'http'
当您调用帖子的the_content
时会触发此过滤器。使用add_filter( 'the_content', 'old_wp_content' );
function old_wp_content( $content ) {
if( get_the_date('Y-m-d') < "2017-02-28" ) {
$content = "<p>This article was originally posted at xyz.com.</p>" . $content;
}
return $content;
}
过滤器,您可以调整the_content()
函数的返回值。
答案 1 :(得分:1)
我终于明白了。额外的&#34;如果&#34;在get_the_date
语句之前,可能是双引号(交换为单引号)包装插入文本的p标签是罪魁祸首。以下代码有效:
function old_wp_content( $content ) {
if (get_the_date('Y-m-d') < '2017-02-28' ) {
$content = $content . '<p>This article was originally posted at <a
rel="canonical" href="#">xyz.com</a>.</p>';
}
return $content;
}
add_filter( 'the_content', 'old_wp_content' );
@kevinvhengst,再次感谢您的时间和耐心帮助我解决这个问题!