怎样给WordPress增加短代码(Shortcode)?

WordPress短代码是比较常用并且便于快速书写文章的代码,可以实现一句很短的代码来表达一个复杂的内容,所以称之为短代码:Shortcode。

怎样给WordPress增加短代码(Shortcode)?
怎样给WordPress增加短代码(Shortcode)?

那么,怎样给WordPress增加短代码(Shortcode)呢?

先说短代码的使用

如果我们创建了“your_shortcode”短代码,那么可以在主题中以

<?php echo do_shortcode("[your_shortcode]"); ?>

来调用。如果是在编辑器代码模式中使用,那么直接用 [your_shortcode] 即可。

一个短代码例子

functions.php中加入:

// function that runs when shortcode is called
function wpb_demo_shortcode() { 

// Things that you want to do. 
$message = 'Hello world!'; 

// Output needs to be return
return $message;
} 
// register shortcode
add_shortcode('greeting', 'wpb_demo_shortcode'); 

这样,在主题中可以用

<?php echo do_shortcode("[greeting]"); ?>

调用,在代码模式中可以用 [greeting] 调用,输出得到 Hello world!

以上方法是在经典编辑器下使用,目前古滕堡编辑器不太好用,可以将古滕堡编辑器禁用掉

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注