第一步:在下方的函式中,dh_first_shortcode 是可以自定义的名称,但要注意如果有更改,函式结尾处的add_shortcode 内容也必须要替换成一样的名称。而这段函式所代表的意义就是定义当dh_first_shortcode 这段文字以代码的形式在HTML 中出现时,将自动输出(echo)文字” shortcode教学” 以及档案位置为' http://demo...
add_shortcode('email',function($atts,$content=''){extract(shortcode_atts(array('link'=>'0'),$atts));if($link){return''.antispambot($content,0).'';}else{returnantispambot($content,0);}}); Shortcode 高级使用技巧: 只有Shortcode 存在时才载入相关脚本文件,我们可以使用has_shortcode()这个...
就像在某些编程语言中使用反斜杠转义字符一样,这样可以直接显示简码,而不会展示它实际链接的内容。 注册简码的代码使用了add_shortcode()方法,其中需要指定简码的标识符(在这个例子中是“pdf”),以及用于处理简码的方法。我们先让它把简码替换成“Hello World!”作为一个初步示例。 点击此处查看代码 插入后,网页...
add_theme_support('post-thumbnails'); // 注册简码以显示产品分类 function my_custom_product_categories_shortcode() { $categories = get_terms('product_cat', array( 'hide_empty'=>false, )); $current_category = single_cat_title('',false); $output =''; foreach($categoriesas$category) { ...
add_shortcode('xz', 'myName'); 编辑文章时我们输入: [xz a="1" b="2" c="3"] 在函数中我们将得到: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. //$array 是一个数组, //大体结构如下 $array = array(‘a’=>‘1’,‘b’=>‘2’,‘c’=>‘3’); ...
这个Shortcode,它的内容($content)就是邮箱地址,还定义了属性$link,它的值为 1 时候,邮箱显示为可点击,详细代码如下: add_shortcode('email', function($atts, $content='') { extract( shortcode_atts( array( 'link' => '0' ), $atts ) ); ...
// $content 是 shortcode 中包含的字符串 // 对 $attr 和 $content 进行处理 // 返回预期的值 } 然后把自己定义的 Shortcode 和其处理函数管理起来,以便 [mycode attr="value"]content[/mycode] 能够按照预期执行。 1 add_shortcode('mycode', 'my_shortcode_func') ...
add_shortcode("msc", "myshortcode_function"); // 注册该 shortcode,以后使用 [msc] 标签调用该 shortcode msc 即为短代码名,以后在写文章或页面时可以直接使用 [msc][/msc] 标签调用该短代码,而 "myshortcode_function" 即为例子中的短代码处理函数的名称。下面重点分析短代码处理函数。
Shortcode 很方便,但是只能用在日志内容中,那么如何在 WordPress 的侧边栏的 Widgets 中使用 Shortcode,在当前主题的functions.php中添加如下代码: add_filter('widget_text','do_shortcode'); 然后你在 WordPress 后台 > 外观 > Widgets 界面添加一个文本 Widget,然后插入博客中经启用 shortcode 即可。
Step 1: Add the Shortcode in Your Theme’s functions.php File You’ll be using theadd_shortcodefunction to give your shortcode a name, and tell WordPress what it should do any time the shortcode is used.Add this line to your theme’sfunctions.phpfile: ...