首页 > 解决方案 > 在 Wordpress 中以编程方式获取给定请求的内容

问题描述

是否可以在代码中以编程方式获取 Wordpress 站点为特定请求返回的内容。

在伪代码中:

我的 wp-template.php

request = get_the_request() // www.example.com/foo/bar

content = get_the_content(request) // the content returned if I were to go to www.example.com/foo/bar in the browser

基本上,它就像一个 curl 请求,但使用的是 Wordpress 内部结构。

你为什么想做这个?

我正在将表单中的请求路由example.com/foo/barmy-wp-template.php

我希望该模板能够正常运行,以便它返回 的内容exmaple.com/bar,就好像请求是在没有/foo/slug 的情况下发出的。

重定向在我的用例中不起作用

标签: phpwordpress

解决方案


这可以通过首先使用url_to_postid()从您想要的路由中检索帖子 ID 来完成,然后使用该 ID 使用get_the_content()获取此帖子/页面的内容。这是如何:

$content_of_foo_bar = get_the_content(null, false, url_to_postid( 'www.example.com/foo/bar' ));

推荐阅读