php - 在 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/bar
到my-wp-template.php。
我希望该模板能够正常运行,以便它返回 的内容exmaple.com/bar
,就好像请求是在没有/foo/
slug 的情况下发出的。
重定向在我的用例中不起作用。
解决方案
这可以通过首先使用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' ));
推荐阅读
- uml - 什么是序列图中的非瞬时和持续时间消息
- python - 在“用户:”之后使用正则表达式获取所有用户
- asp.net-mvc - MVC:当ModelState在Controller中有效时显示一个弹出窗口
- java - 使用哪一个:HashMap 和 ConcurrentHashMap 与未来值 JAVA?
- javascript - 有没有更好的迭代和比较 2 个数组中的对象的方法?
- gatsby - Gatsby 和 Leaflet:检查 `LeafletMap` 的渲染方法
- python - 如何正确操作这里的文件?
- interpolation - Regrid,从 0.5 度到 0.05 度 CDO/NCO
- ruby-on-rails - 刺激反射访问应用程序控制器变量
- python - 使用 try-except 迭代编写文本文件的问题