如何创建自己的WordPress短代码

WordPress doesn’t normally allow you to add PHP code to pages or posts. That’s for the best; you don’t want clients to discover the power of the unlink function! However, you can create custom functions which are executed when a shortcode is encountered within the post text.

WordPress通常不允许您将PHP代码添加到页面或帖子中。 那是最好的。 您不希望客户发现取消链接功能的强大功能! 但是,您可以创建自定义函数,这些函数将在发布文本中遇到短代码时执行。

简单的简码 (Simple shortcodes)

Shortcode functions can be added to plugin code or your theme’s functions.php file. If it’s the latter, I’d recommend creating a separate shortcodes.php file, then adding include('shortcodes.php'); to functions.php.

可以将简码功能添加到插件代码或主题的functions.php文件中。 如果是后者,建议您创建一个单独的shortcodes.php文件,然后添加include('shortcodes.php'); 到functions.php。

Here’s a basic “Hello World” example:

这是一个基本的“ Hello World”示例:

function HelloWorldShortcode() {
	return '<p>Hello World!</p>';
}
add_shortcode('helloworld', 'HelloWorldShortcode');

Enter [helloworld] somewhere within a page or post to output the result of the HelloWorldShortcode() function.

在页面中的某处输入[helloworld]或发布以输出HelloWorldShortcode()函数的结果。

参数化的简码 (Parameterized shortcodes)

The following shortcode function generates a page hierarchy sitemap. Three optional parameters can be passed: a title, the ID of the resulting ul list, and a depth value indicating the number of page navigation levels.

以下简码函数生成页面层次结构站点地图。 可以传递三个可选参数:标题,结果ul列表的ID和指示页面导航级别数的深度值。

function GenerateSitemap($params = array()) {

	// default parameters
	extract(shortcode_atts(array(
		'title' => 'Site map',
		'id' => 'sitemap',
	    'depth' => 2
	), $params));

	// create sitemap
	$sitemap = wp_list_pages("title_li=&depth=$depth&sort_column=menu_order&echo=0");
	if ($sitemap != '') {
		$sitemap =
			($title == '' ? '' : "<h2>$title</h2>") .
			'<ul' . ($id == '' ? '' : " id="$id"") . ">$sitemap</ul>";
	}

	return $sitemap;
}
add_shortcode('sitemap', 'GenerateSitemap');

A custom sitemap can be added to any page using a shortcode such as [sitemap id='deepmap',depth=5].

可以使用[sitemap id='deepmap',depth=5]例如[sitemap id='deepmap',depth=5]将自定义站点地图添加到任何页面。

BB代码简码 (BB code shortcode)

The final way to add shortcodes uses [bbcode]BB code syntax[/bbcode]:

添加简码的最后一种方法是使用[bbcode] BB代码语法[/ bbcode]:

function StyleText($params, $content = null) {

	// default parameters
	extract(shortcode_atts(array(
		'style' => ''
	), $params));

  return
	'<span' .
	($style == '' ? '' : " style="$style"") .
	">$content</span>";
}
add_shortcode('format','StyleText');

This function allows the author to embed CSS styles within their article, e.g. [format style="font-size:1.5em;color:#f00;">Important![/format]. Perhaps that’s not such a great idea!…

此功能允许作者将CSS样式嵌入其文章中,例如[format style="font-size:1.5em;color:#f00;">Important![/format] 。 也许那不是一个好主意!…

Have you seen any interesting uses for shortcodes within WordPress projects?

您是否看到WordPress项目中的短代码有任何有趣的用途?

翻译自: https://www.sitepoint.com/wordpress-shortcodes-tutorial/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值