如何在Wordpress中创建简单的自定义短代码?
问题描述:
我在/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]。
我想我需要将页面类型链接到插件或类似的东西。你能帮我吗?
答
在/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
在哪里,你如何在页面中添加[customshortcode]?在你的'test.php'中写入 –
写:'return'
';'。为什么你要包含'/ wp-includes/shortcodes.php'? –因为没有它,当我到main.php在URL栏中运行它时,我得到: 致命错误:调用未定义的函数add_shortcode()在C:\ MAMP \ htdocs \ new \ wp-content \ plugins \第8行的customshortcode \ main.php – GRS