【问题标题】:How to create a simple custom shortcode in Wordpress?如何在 Wordpress 中创建简单的自定义简码?
【发布时间】:2017-03-07 10:18:56
【问题描述】:

我在/wp-content/plugins 目录中创建了一个名为customshortcode 的文件夹。 add_shortcode command 应该将短代码与函数链接起来。

/wp-content/plugins/customshorcode/我创建了2个文件:main.phptest.php

主要的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]。

我想我需要以某种方式将页面类型链接到插件或类似的东西。你能帮帮我吗?

【问题讨论】:

  • 在页面中添加 [customshortcode] 的位置和方式?
  • 在您的test.php 中写:return '&lt;style&gt;.testdiv{border:1px solid black; width:300px; heigh:300px;}&lt;/style&gt;&lt;div class="testdiv"&gt;&lt;/div&gt;';。以及为什么包含/wp-includes/shortcodes.php??
  • 因为没有它,当我到达 main.php 并在 URL 栏中运行它时,我得到:致命错误:调用 C:\MAMP\htdocs\new\ 中的未定义函数 add_shortcode() wp-content\plugins\customshortcode\main.php 在第 8 行
  • 也在 test.php 我需要打开和关闭 标签吗?目前我只有上面没有标签的return语句。
  • @SamuelJMathew 我只是创建了一个新页面,添加了一个标题,并将短代码放入内容中,点击预览页面

标签: php wordpress


【解决方案1】:

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

<?php
/*
* Plugin Name: Custom Short Code
* Description: Create your WordPress shortcode.
* Version: 1.0
* Author: samuelj90@gmail.com 
*/

// 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

【讨论】:

  • 谢谢,效果很好!我不知道我必须创建这样的插件,我在文档中找不到它
【解决方案2】:
   <?php
/*
* Plugin Name: Custom ShortCodes
* Description: Create your WordPress plugin shortcodes.
* Version: 1.0
* Author: fatir031624@gmail.com
*/

function wpb_test_shortcode() { 
$message = 'Hello world!'; 
return $message;
} 
add_shortcode('testing', 'wpb_test_shortcode');
?>

【讨论】:

  • 详细回答在您访问的网站。URL:devndesigns.com
  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-05
  • 1970-01-01
  • 2023-03-04
  • 2019-07-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多