[英]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.