在 WordPress Elementor 中透過 shortcode 加入自訂的 PHP

分享:

一般要客製化 WordPress 樣板我們可以選擇自己修改 PHP 或者透過 Elementor 這類的所見即所得編輯器來編輯,而如果想要修改已經套用 Elementor  的頁面,則可以使用 shortcode 來讓我們把自訂的 PHP 程式碼放到 Elementor 中,使用方法如下:

Step1:在 functions.php 的檔案中加入自訂的 PHP 程式碼:

// Shortcode to output custom PHP in Elementor
function wpc_elementor_shortcode( $atts ) {
    echo "This is my custom PHP output in Elementor!";
}
add_shortcode( 'my_elementor_php_output', 'wpc_elementor_shortcode');

其中 my_elementor_php_output 是用來識別自訂程式碼的名稱,以及內容可以修改你要的樣子。

 

Step2:在 Elementor 編輯器加入 shortcode

接著在 Elementor 找到短代碼,拖曳至頁面中

Elementor shortcode

接著加入 shortcode:[my_elementor_php_output],這樣就能把自己撰寫的 PHP 程式碼加入頁面了。

 

課程推薦

和我們交流

加入我們的社群,裡面會有一些技術的內容、有趣的技術梗,以及職缺的分享,歡迎和我們一起討論。