简体   繁体   English

将简码添加到 WP (functions.php)

[英]Adding Shortcode to WP (functions.php)

WordPress and PHP newbie here. WordPress 和 PHP 新手在这里。

I just want to add the most basic shortcode directly into functions.php for learning purposes.我只想将最基本的短代码直接添加到functions.php中。php 以供学习之用。 My shortcode does not work, basically, nothing happens.我的简码不起作用,基本上,没有任何反应。 If I am able to run this simple code, then I will try more complex shortcodes.如果我能够运行这个简单的代码,那么我会尝试更复杂的短代码。

function hello_world() {
  return 'Hello World!';
}

add_shortcode('helloworld', 'hello_world');

In a page, I add [helloworld] using the Gutenberg shortcode block.在页面中,我使用 Gutenberg 短代码块添加[helloworld] I cannot see any errors on the page or console.我在页面或控制台上看不到任何错误。 Am I missing something fundamental to add shortcodes?我是否缺少添加短代码的基本内容?

try the following:尝试以下操作:

function hello_world() {
  return '<h1>Hello World!</h1>';
}

function register_shortcodes(){
   add_shortcode('helloworld', 'hello_world');
}

add_action( 'init', 'register_shortcodes');

In order to execute our register_shortcodes() function, you will tie it to WordPress' initialization action.为了执行我们的 register_shortcodes() function,您将把它绑定到 WordPress 的初始化操作。

Then try your shortcode with [helloworld]然后用[helloworld]试试你的简码

Please let me know if it works请让我知道它是否有效

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

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