简体   繁体   English

在 elementor 小部件中显示 wordpress 短代码?

[英]Display a wordpress shortcode in elementor widget?

I'm not familiar with WordPress yet, I want to display a shortcode in the widget itself but it shows it as #text我还不熟悉 WordPress,我想在小部件本身中显示一个短代码,但它显示为#text

<li><span>[My-shortcode].. 

As I understand, I must edit the code of the custom theme, but where do I start?据我了解,我必须编辑自定义主题的代码,但我从哪里开始呢? Maybe there are some hacks-bypasses to make it work?也许有一些黑客绕过来使它工作? Plugins that auto-detect text as shortcode?将文本自动检测为短代码的插件? If I could type PHP in Wp Editor I could make it work, any ideas?如果我可以在 Wp 编辑器中输入 PHP,我可以让它工作,有什么想法吗?

First of all: no, you cannot type PHP code into the WP editor.首先:不,您不能在 WP 编辑器中键入 PHP 代码。 The most common ways of writing custom code in WordPress are:在 WordPress 中编写自定义代码的最常见方法是:

  1. Creating your own plugin创建自己的插件
  2. Creating a child theme, modifying the child theme's functions.php创建子主题,修改子主题的functions.php

For the second option, please follow the official guide at https://developer.wordpress.org/themes/advanced-topics/child-themes/ .对于第二个选项,请遵循https://developer.wordpress.org/themes/advanced-topics/child-themes/ 上的官方指南。

Once you have it set up, activated, and have a custom empty functions.php file inside your child theme's directory, you can open the functions.php file and add the following code:设置、激活并在子主题目录中创建自定义的空 functions.php 文件后,您可以打开 functions.php 文件并添加以下代码:

function mytextdomain_myshortcode($attrs = array(), $content = null)
{
  $attrs = shortcode_atts( array(
      'value' => 'content'
  ), $attrs );
  ob_start();
?>
Write your own HTML code outputting <strong>some <?= $attrs['value'] ?></strong> here!
<?php
  return ob_get_clean();
}

// add the custom shortcode:
add_shortcode('myshortcode', 'mytextdomain_myshortcode');

The mytextdomain_myshortcode callback function will be evaluated each time a shortcode is rendered.每次呈现简码时都会评估mytextdomain_myshortcode回调函数。 You can rename that function to whatever you want - the text domain should be something that makes the name unique.您可以将该函数重命名为您想要的任何名称 - 文本域应该是使名称唯一的内容。 What kind of HTML you want to output depends on what you want it to do.你想要输出什么样的 HTML 取决于你想要它做什么。

After this, you can use [myshortcode] , or [myshortcode value="custom text"] in any place where shortcodes are evaluated.在此之后,您可以在评估短代码的任何地方使用[myshortcode][myshortcode value="custom text"] You can add arguments depending on the functionality you want to implement.您可以根据要实现的功能添加参数。 Do not use hyphens in shortcode names.不要在短代码名称中使用连字符。 You can also simply turn this into an enclosing shortcode by outputting the $content .您也可以通过输出$content将其简单地转换为封闭的短代码。 In case of [myshortcode]This will end up in content![/myshortcode] , $content will contain everything between the shortcode tags.如果是[myshortcode]This will end up in content![/myshortcode]$content将包含短代码标签之间的所有$content Or you can just decide to ignore content all along.或者您可以决定一直忽略内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM