在所见即所得编辑器中运行 php 代码
我安装了 execPHP 插件。就通过编辑器执行 php 代码而言,没有任何问题...例如,我在 WYSIWYG 编辑器中尝试了以下 PHP 代码片段,并且它有效...
[?php echo get_post_meta(1717, 'Ref', true); ?]
它输出存储在标题为 REF 的自定义字段中的值。而 1717 恰好是我正在编辑的帖子的当前帖子 ID。
这是问题...
每当我尝试访问与帖子相关的数据(通常在 WordPress 循环中可用)时,都不会输出任何内容。例如,以下内容不会产生任何结果...
[?php echo ("postid=" . $post->ID ); ?]
在 WYSIWYG 编辑器中,甚至 the_ID() 也不会输出任何内容。我想,我们仍然处于 WordPress 循环中,我现在可以利用任何与帖子相关的数据。
我的目标是替换 [?php echo get_post_meta(1717, 'Ref', true); ?]
与 [?php echo get_post_meta($post->ID, 'Ref', true); ?]
我做错了什么?
I got the execPHP plug in installed. no problems with it as far as executing the php code thru the editor... for example, I tried the following the PHP snippet within the WYSIWYG editor and it works...
[?php echo get_post_meta(1717, 'Ref', true); ?]
it outputs the value stored within the custom field titled REF. And the 1717 happens to be the current post id of the post I was editing.
here is the question...
whenever I tried to access post related data ( usually available in the wordpress loop ), nothing gets outputted. for example, the following produces nothing...
[?php echo ("postid=" . $post->ID ); ?]
Within the WYSIWYG editor, even the the_ID() outputs nothing. I thought, we were still in the wordpress loop and I could tap into any post related data at the moment.
my goal was to replace the [?php echo get_post_meta(1717, 'Ref', true); ?]
with [?php echo get_post_meta($post->ID, 'Ref', true); ?]
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试使用 wordpress 函数
get_the_ID()
而不是访问 $post 变量。我想如果你把全局 $post 也可以访问 $post
You can try the wordpress function
get_the_ID()
instead of accessing the $post variable.I guess you can access $post also if you put global $post