WordPress开发中短代码的实现及相关函数使用技巧,wordpress使用技巧
WordPress开发中短代码的实现及相关函数使用技巧,wordpress使用技巧
其实实现短代码很简单,我们只需要用到 WordPress 里面的一个函数就可以搞定短代码,外加自己的一个小函数,可以让短代码实现的轻松加愉快。
短代码实现原理
就像往 WP 一些动作里加钩子和过滤函数一样,
短代码只是经过封装了的针对文章输出内容的过滤器而已,
没有像有一些主题功能说的那么震撼、那么高深。
下面来一个简单例子:
function myName() {//短代码要处理的函数 return "My name's XiangZi !"; } //挂载短代码 //xz为短代码名称 //即你在编辑文章时输入[xz]就会执行 myName 函数 add_shortcode('xz', 'myName');
那么我们在文章中输入[xz]就会得到
My name's XiangZi !
短代码传参
更高深一点的利用,我将会在后面的文章中讲到,
今天只讲一下,短代码的传参机制
高级一点的例子
function myName($array,$content) { var_dump($array); var_dump($content); } add_shortcode('xz', 'myName');
编辑文章时我们输入:
[xz a="1" b="2" c="3"]这里是三个参数哦[/xz]
在函数中我们将得到:
//$array 是一个数组, //大体结构如下 $array = array('a'=>'1','b'=>'2','c'=>'3'); //$content 是一个字符串 $content = '这里是三个参数哦';
shortcode_atts
不是因为搞短代码插件,我也不会用到这个函数,
shortcode_atts 函数主要是用来设置短代码中截获变量的初始值。
这是一个很实用的函数,其实这个函数的真正是作用在数组上得,
因为我们从短代码中截获的参数都是数组形式的。
shortcode_atts 函数详解
不要被函数名所疑惑,在 WordPress 里主要是用于设置短代码参数的默认值,
如果我们将代码提取出来,用在别的地方,该函数可以帮我们设置一个既得数组的默认值。
shortcode_atts 函数使用
这个函数使用起来很简单。
shortcode_atts(array( "url" => 'http://PangBu.Com' ), $url)
以上代码的意思是,
将 $url 数组 键值为url的成员默认值设定为'http://PangBu.Com',
别的地方用处似乎不多,但对于一些超级懒人,有时候揽到总是忘记或是懒得设定数组的数值时,这个函数超好用。
shortcode_atts 函数声明
/** * Combine user attributes with known attributes and fill in defaults when needed. * * The pairs should be considered to be all of the attributes which are * supported by the caller and given as a list. The returned attributes will * only contain the attributes in the $pairs list. * * If the $atts list has unsupported attributes, then they will be ignored and * removed from the final returned list. * * @since 2.5 * * @param array $pairs Entire list of supported attributes and their defaults. * @param array $atts User defined attributes in shortcode tag. * @return array Combined and filtered attribute list. */ function shortcode_atts($pairs, $atts) { $atts = (array)$atts; $out = array(); foreach($pairs as $name => $default) { if ( array_key_exists($name, $atts) ) $out[$name] = $atts[$name]; else $out[$name] = $default; } return $out; }
您可能感兴趣的文章:
- WordPress开发中用于获取近期文章的PHP函数使用解析
- WordPress开发中自定义菜单的相关PHP函数使用简介
- WordPress中用于获取搜索表单的PHP函数使用解析
- 在WordPress中使用wp_count_posts函数来统计文章数量
- 详解WordPress中调用评论模板和循环输出评论的PHP函数
- 在WordPress中加入Google搜索功能的简单步骤讲解
- 详解WordPress开发中的get_post与get_posts函数使用
- 解析WordPress中的post_class与get_post_class函数
- WordPress开发中的get_post_custom()函数使用解析
- 在WordPress中安装使用视频播放器插件Hana Flv Player
- 详解WordPress中分类函数wp_list_categories的使用

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP and Flutter are popular technologies for mobile development. Flutter excels in cross-platform capabilities, performance and user interface, and is suitable for applications that require high performance, cross-platform and customized UI. PHP is suitable for server-side applications with lower performance and not cross-platform.

You can easily modify your WordPress page width by editing your style.css file: Edit your style.css file and add .site-content { max-width: [your preferred width]; }. Edit [your preferred width] to set the page width. Save changes and clear cache (optional).

Create a product page in WordPress: 1. Create the product (name, description, pictures); 2. Customize the page template (add title, description, pictures, buttons); 3. Enter product information (stock, size, weight); 4 . Create variations (different colors, sizes); 5. Set visibility (public or hidden); 6. Enable/disable comments; 7. Preview and publish the page.

WordPress posts are stored in the /wp-content/uploads folder. This folder uses subfolders to categorize different types of uploads, including articles organized by year, month, and article ID. Article files are stored in plain text format (.txt), and the filename usually includes its ID and title.

WordPress template files are located in the /wp-content/themes/[theme name]/ directory. They are used to determine the appearance and functionality of the website, including header (header.php), footer (footer.php), main template (index.php), single article (single.php), page (page.php), Archive (archive.php), category (category.php), tag (tag.php), search (search.php) and 404 error page (404.php). By editing and modifying these files, you can customize the appearance of your WordPress website

Search for authors in WordPress: 1. Once logged in to your admin panel, navigate to Posts or Pages, enter the author name using the search bar, and select Author in Filters. 2. Other tips: Use wildcards to broaden your search, use operators to combine criteria, or enter author IDs to search for articles.

The most stable WordPress version is the latest version because it contains the latest security patches, performance enhancements, and introduces new features and improvements. In order to update to the latest version, log into your WordPress dashboard, go to the Updates page and click Update Now.

WordPress is developed using PHP language as its core programming language for handling database interactions, form processing, dynamic content generation, and user requests. PHP was chosen for reasons including cross-platform compatibility, ease of learning, active community, and rich library and frameworks. Apart from PHP, WordPress also uses languages like HTML, CSS, JavaScript, SQL, etc. to enhance its functionality.
