How To Make A Tag Cloud

Table of contents:

How To Make A Tag Cloud
How To Make A Tag Cloud

Video: How To Make A Tag Cloud

Video: How To Make A Tag Cloud
Video: Word Cloud Generator - Make Word Clouds For FREE ! 2024, May
Anonim

Surely on many sites and blogs you have come across such a funny thing as a tag cloud. If you want to create such a cloud on your website or blog, then in this article you can find instructions on how to generate it.

A tag cloud is a collection of tags or categories that appear most frequently in searches, links, and word mentions. And the more often one or another word or concept occurs, the larger the image becomes, containing a hyperlink to these concepts and words.

How to make a tag cloud
How to make a tag cloud

Instructions

Step 1

First, generate a list of pairs of the form: tag - the number of occurrences. To do this, use the following code:

1. <?

2. class TagsCloud {

3.

4. private $ tags;

5.

6.private $ font_size_min = 14;

7.private $ font_size_step = 5;

8.

9.function _construct ($ tags) {

10.

11.shuffle ($ tags);

12. $ this-> tags = $ tags;

13.

14. }

15.

16.private function get_tag_count ($ tag_name, $ tags) {

17.

18. $ count = 0;

19.

20. foreach ($ tags as $ tag) {

21. if ($ tag == $ tag_name) {

22. $ count ++;

23. }

24. }

25.

26. return $ count;

27.

28. }

29.

30. private function tagscloud ($ tags) {

31.

32. $ tags_list = array ();

33.

34. foreach ($ tags as $ tag) {

35. $ tags_list [$ tag] = self:: get_tag_count ($ tag, $ tags);

36. }

37.

38. return $ tags_list;

39.

40. }

41.

42. private function get_min_count ($ tags_list) {

43.

44. $ min = $ tags_list [$ this-> tags [0];

45.

46. foreach ($ tags_list as $ tag_count) {

47.

48. if ($ tag_count <$ min) $ min = $ tag_count;

49.

50. }

51.

52. return $ min;

53.

54. }

55.

56. public function get_cloud () {

57.

58. $ cloud = Array ();

59.

60. $ tags_list = self:: tagscloud ($ this-> tags);

61. $ min_count = self:: get_min_count ($ tags_list);

62.

63. foreach ($ tags_list as $ tag => $ count) {

64.

65. $ font_steps = $ count - $ min_count;

66. $ font_size = $ this-> font_size_min + $ this-> font_size_step * $ font_steps;

67.

68. $ cloud = ". $ Tag. ";

69. }

70.

71. return $ cloud;

72.

73. }

74. }

75. ?>

Step 2

Work with this code like this:

01. <?

02.

03. $ tags = array (04. '1111', '2222', '333', '444', 05. '5555', '666', '777', '777', 06. '333', '8888', '6666', '333', 07. '888', '000’, '989', '45455', 08. '5555', '63636', '54545', '55656'

09.);

10.

11. $ mycloud = new TagsCloud ($ tags);

12. $ tags_list = $ mycloud-> get_cloud ();

13.

14.foreach ($ tags_list as $ tag) {

15.echo $ tag. ' ';

16. }

17.

18. ?>

In place of the numbers, put the tags you need!

Step 3

To display a tag cloud based on relevance, do the following. In the settings, set the minimum font size that will correspond to the minimum number of occurrences (that is, one occurrence). Use the font_size_min field.

Step 4

Give the rest of the tags the font size as follows: each occurrence increases the font by one pixel. To do this, use the font_size_step field.

Step 5

If you want to improve your tag cloud, link it to a js script.

Good luck creating your own clouds!

Recommended: