2017-03-07 150 views
0

我在/wp-content/plugins目錄下創建了一個名爲customshortcode的文件夾。 add_shortcode command應該將短代碼與函數鏈接起來。如何在Wordpress中創建簡單的自定義短代碼?

/wp-content/plugins/customshorcode/我創建2個文件:main.php和主要的PHP test.php

內容是:

<?php require_once('../../../wp-includes/shortcodes.php'); ?> 

<?php function custom_shortcode_func() { 
    include_once('test.php'); 
} 

add_shortcode('customshortcode','custom_shortcode_func'); 

?> 

而且test.php的只是顯示一個分割塊:

<style>.testdiv{border:1px solid black; width:300px; heigh:300px;}</style> 

<div class="testdiv"></div> 

我第一運行/wp-content/plugins/customshorcode/main.php文件以查看沒有錯誤和簡單的白色屏幕。我認爲應該保存短代碼。

但是,當我在頁面中輸入[customshortcode]時,我沒有得到顯示的部分,而是改爲使用文本[customshortcode]。

我想我需要將頁面類型鏈接到插件或類似的東西。你能幫我嗎?

+0

在哪裏,你如何在頁面中添加[customshortcode]?在你的'test.php'中寫入 –

+0

寫:'return'

';'。爲什麼你要包含'/ wp-includes/shortcodes.php'? –

+0

因爲沒有它,當我到main.php在URL欄中運行它時,我得到: 致命錯誤:調用未定義的函數add_shortcode()在C:\ MAMP \ htdocs \ new \ wp-content \ plugins \第8行的customshortcode \ main.php – GRS

回答

1

在/wp-content/plugins/customshorcode/main.php

<?php 
/* 
* Plugin Name: Custom Short Code 
* Description: Create your WordPress shortcode. 
* Version: 1.0 
* Author: [email protected] 
*/ 

// Example 1 : WP Shortcode to display form on any page or post. 
function custom_shortcode_func() { 
?> 
<style>.testdiv{border:1px solid black; width:300px; heigh:300px;}</style> 

<div class="testdiv"></div> 
<?php 
} 
add_shortcode('customshortcode','custom_shortcode_func'); 

?> 

然後啓用該插件

Custom Short Code 
+0

謝謝,它的效果非常好!我不知道我必須創建一個這樣的插件,我無法在文檔中找到它 – GRS

相關問題