For some wordpress sites, due to the large number of categories, it is necessary to call popular articles that specify the category directory or TAG. Some functions of the WP-Postviews plug-in are needed here: get_most_viewed (), get_most_viewed_category (), and get_most_viewed_tag (). This makes it easy for us to directly call related hot articles by opening the theme file sidebar. php file, add the following code to the desired location:
Displays the most frequently viewed articles or pages, including your pages, such as personal introductions and copyright descriptions. By default, 10 articles or pages are displayed:
The code is as follows: |
Copy code |
<? Php get_most_viewed () ;?> |
Show only the articles with the most browsing times:
The code is as follows: |
Copy code |
<? Php get_most_viewed ('post');?> |
Only 15 articles with the most browsing times are displayed:
The code is as follows: |
Copy code |
<? Php get_most_viewed ('post', 15);?> |
I believe that through the above examples, I have some knowledge about the functions of the WP-Postviews plug-in.
Next, we will introduce how Wordpress calls the popular articles of the current category directory or the current TAG label. First, put the following code in the topic file functions. php:
The code is as follows: |
Copy code |
Function get_current_category_id (){ $ Current_category = single_cat_title (", false); // Obtain the name of the current category directory Return get_cat_ID ($ current_category); // obtain the ID of the current category directory } Function get_current_tag_id (){ $ Current_tag = single_tag_title (", false); // Obtain the current TAG name $ Tags = get_tags (); // array for obtaining information about all TAG tags Foreach ($ tags as $ tag ){ If ($ tag-> name ==$ current_tag) return $ tag-> term_id; // Obtain the current tag id, where term_id is the tag ID } }
|
This step is to obtain the specified category Directory ID or the specified tag id. Then, you can use the following code to call the functions in the functions file to implement the desired functions:
The code is as follows: |
Copy code |
<? Php If (is_category ()) { Get_most_viewed_category (get_current_category_id ()); } Elseif (is_tag ()) { Get_most_viewed_tag (get_current_tag_id ()); } Else { Get_most_viewed (); } ?>
|
OK. The method for Wordpress to call the popular articles about the current category directory or the current TAG is described.
Supplement: replace the most in the above code with least to call the unpopular article.
Add an article page and call tag and category directory for keywords.
The code is as follows: |
Copy code |
$ Keywords = ''; $ Tags = wp_get_post_tags ($ post-> ID ); Foreach ($ tags as $ tag ){ $ Keywords. = $ keywords. $ tag-> name .','; } // $ Post-> ID is optional. The current article ID is selected by default. Foreach (get_the_category ($ post-> ID) as $ category ){ $ Keywords. = $ category-> cat_name .','; } // Clear the last comma of the keyword $ Keywords = rtrim ($ keywords ,','); // Add the two rows $ KeywordsArray = array_unique (explode (',', $ keywords )); $ Keywords = implode (',', $ keywordsArray ); |