再次在WordPress中创建快捷码

时间:2011-07-14 作者:pereyra

这里和互联网上都有很多例子,我已经尝试过了,但似乎无法实现我所需要的,尽管我觉得应该足够简单。

基本上,我希望能够使用这样的短代码:

[download]http://site.com/file.doc[/download]
我需要Wordpress输出这个:

<a class="download" href="http://site.com/file.doc">download file</a>
<a class="download" href="http://docs.google.com/viewer?embedded=true&url=http://site.com/file.doc">preview file</a>
非常感谢您的帮助。非常感谢!

1 个回复
最合适的回答,由SO网友:David 整理而成
// Declare your shortcode
add_shortcode("download", "downloadFunction");

// Second Declare your shortcode function
function downloadFunction($att, $content, $code){

    // your function is passed 3 Arguments
    // $atts : this is an array of the shortcode\'s attributes
    // $content : this is the content between the code. in your case the file url.
    // $code : this is the code used. in this case it will be download.
    // here we only need the $content of the shortcode which we place for the file

$return = \'<a class="download" href="\'.$content.\'">download file</a>\';
$return .= \'<a class="download" href="http://docs.google.com/viewer?embedded=true&url=\'.$content.\'">preview file</a>\';

// we then return the result.
return $return;
}
结束

相关推荐

Nested Shortcode Detection

如果您熟悉此代码<?php $pattern = get_shortcode_regex(); preg_match(\'/\'.$pattern.\'/s\', $posts[0]->post_content, $matches); if (is_array($matches) && $matches[2] == \'YOURSHORTCODE\') { //shortcode is being used }&#