[英]How do I get output from php file to display in a div on the wordpress page that called it?
[英]How do I insert output from a PHP return into an HTML <div>? (I'm using WordPress)
我创建了一个快速的Wordpress简码,以便我的作者可以在帖子的可视化编辑器中轻松设置字幕的样式:
function subtitle( $atts, $content = null ) {
return '<p class="subtitle">' . $content . '</p>';
}
我想将此输出移到文档中其他位置的指定内部,例如:
<div id="output_here_please"></div>
<?php the_content(); /* It is returned here wherever they place the shortcode */ ?>
做这个的最好方式是什么? 是否可以使用PHP或WordPress函数,还是应该创建一个JavaScript来替换innerHTML(我尝试过此操作,但不知道在页面加载后如何将其插入)。
谢谢
如果您的函数像这样被称为字幕,那么他们可以使用以下命令插入它:
<div id="output_here_please">
<?php subtitle("value", "This is some sample content"); ?>
</div>
快速而肮脏的方法是声明一个全局变量,例如$subtitle_content
,并使用它临时存储要显示在其他位置的html片段。
function subtitle( $atts, $content = null ) {
$GLOBALS["subtitle_content"] = '<p class="subtitle">' . $content . '</p>';
return "";
}
function the_content(){
echo $GLOBALS["subtitle_content"];
/* unset $GLOBALS["subtitle_content"]; */
}
如上面注释掉的代码中所写,您可能想要在不再需要该变量时取消定义它。
$GLOBAL
上的文档供您参考。
注意:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.