WordPress has a built-in tag cloud function, which may sometimes be too monotonous. So today we will teach you how to color the WordPress tag cloud!
Plugin version
Download Address: http://downloads.wordpress.org/plugin/colorful-tag-cloud.zip
Code-only teaching
Add the code below to the bottom of the set function library (functions. php)?> Click "update file!
The code is as follows: |
Copy code |
/* Set the color label cloud (created by AREFLY. COM )*/ Function colorful_tag_cloud_callback ($ matches ){ $ Text = $ matches [1]; For ($ a = 0; $ a <6; $ a ++ ){ $ Color. = dechex (rand (0, 15 )); } $ Pattern = '/style = (\' | \ ") (. *) (\ '| \")/I '; $ Text = preg_replace ($ pattern, 'style = "color: # '. $ color.';" ', $ text ); Return "<a". $ text. "> "; Unset ($ color ); } Function colorful_tag_cloud ($ text ){ $ Text = preg_replace_callback ('| <a (. +?)> | I ', 'Colorful _ tag_cloud_callback', $ text ); Return $ text; } Add_filter ('WP _ tag_cloud ', 'Colorful _ tag_cloud', 1 ); |
/* Set the color label cloud to start */
Supplement
Modify the cloud font size, sorting order, and display quantity of wordpress tags.
Next, you need to modify the wordpress program to control the wp_tag_cloud () function for displaying the standard cloud ()
This function is located in the wp-includescategory-template.php file, search wp_tag_cloud to find relevant parameters for modification.
Find the location of the default wp_tag_cloud () function as follows:
The code is as follows: |
Copy code |
Function wp_tag_cloud ($ args = ''){ $ Defaults = array ( 'Smallest' => 8, 'largest '=> 22, 'unit' => 'pt', 'number' => 45, 'Format' => 'flat', 'separator' => "n", 'orderby' => 'name', 'order' => 'asc ', 'Clude' => '', 'include '=>'', 'link' => 'view', 'Taxonomy' => 'Post _ tag ', 'echo '=> true );
|
Wp_tag_cloud () function parameter explanation: (the two lines of code are mainly modified, and others do not need to be ignored)
The code is as follows: |
Copy code |
'Smallest' => 8, 'largest '=> 22, 'unit' => 'pt', 'number' => 45, 'Format' => 'flat', 'separator' => "n", 'orderby' => 'name', 'order' => 'asc ',
|
Examples of wp_tag_cloud () function parameters:
Change to the following style. The display effect is the cloud style of the label of this blog.
The code is as follows: |
Copy code |
'Smallest' => 10, 'largest '=> 14, 'unit' => 'pt', 'number' => 80, 'Format' => 'flat', 'separator' => "n", 'orderby' => 'count', 'order' => 'desc ', |
Smallest: set the font size of the labels displayed in the cloud with the minimum count (used in at least articles). The default value is 8pt.
Largest: set the font size of the tags with the maximum count (maximum number of articles used) among all tags on the tag cloud. The default value is 22pt.
Unit: The unit of the label text font size. The default value is pt, which can be px, em, pt, or percentage;
Number: sets the maximum number of labels displayed in the tag cloud. The default value is 45. If it is set to "0 & Prime;", all labels are called;
Format: The format of the call label. Options include "flat", "list", and "array". The default value is "flat", and "list;
Separator: (string) (optional) text/space between tags. Default value: '/N' (space );
Orderby: Set the sorting method for tags in the cloud. The default value is "name", which is sorted by name. If it is set to "count", it will be sorted by the number of associated articles;
Order: sorting method. The default value is "ASC". In ascending order, "DESC" is in descending order, and "RAND" is in any order;
Exclude: exclude some tags, enter tag IDs, and separate them with commas, for example, "exclude =, & Prime; do not display tags with IDs of 1, 3, 5, and 7;
Include: contains tags, which are used in the same way as exclude. For example, if "include = 2, 4, 6, 8 & Prime;", only tags with IDs of 2, 4, 6, and 8 are displayed;
Link: (string) (optional) sets the link to allow editing of a specified tag. Valid values include: 'View' (default) and 'edit ';
Taxonomy: (string) (optional) used to generate a cloud classification. 'Post _ tag' uses the document tag as the cloud source (default), 'Category 'uses the document classification to generate the cloud, and 'link _ category' uses the link classification directory to generate the cloud;
Echo: (Boolean) (optional) display results, or keep the results in variables. The default value is true (label cloud is displayed ). Valid values include: 1 (true) default value, 0 (false ).
Reminder: the preceding tag parameters are optional and can be used in combination.