[英]How do I insert output from a PHP return into an HTML <div>? (I'm using WordPress)
I created a quick Wordpress shortcode so my writers can easily style out a subtitle in the Visual editor of a post: 我创建了一个快速的Wordpress简码,以便我的作者可以在帖子的可视化编辑器中轻松设置字幕的样式:
function subtitle( $atts, $content = null ) {
return '<p class="subtitle">' . $content . '</p>';
}
I want to move this output to inside a specified elsewhere in the document, example: 我想将此输出移到文档中其他位置的指定内部,例如:
<div id="output_here_please"></div>
<?php the_content(); /* It is returned here wherever they place the shortcode */ ?>
What is the best way to do this? 做这个的最好方式是什么? Is there a PHP or WordPress function to use, or should I create a javascript to replace innerHTML (I tried this but didn't know how to insert it AFTER the page loads).
是否可以使用PHP或WordPress函数,还是应该创建一个JavaScript来替换innerHTML(我尝试过此操作,但不知道在页面加载后如何将其插入)。
Thanks 谢谢
If your function is called subtitle like that, then they can insert it with: 如果您的函数像这样被称为字幕,那么他们可以使用以下命令插入它:
<div id="output_here_please">
<?php subtitle("value", "This is some sample content"); ?>
</div>
The quick and dirty way of doing it is to declare a global variable - say $subtitle_content
, and use it to store temporarily the html fragment you wish to display elsewhere . 快速而肮脏的方法是声明一个全局变量,例如
$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"]; */
}
You may want to undefine that variable once it becomes unnecessary, as written in the commented out code above. 如上面注释掉的代码中所写,您可能想要在不再需要该变量时取消定义它。
Documentation on $GLOBAL
for your information. $GLOBAL
上的文档供您参考。
Note: 注意:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.