The wordpress page displays the specified category of articles:
First, create a special page template, copy the index. php file in the topic, and modify the name of the template you want, such as tips. php.
Add the following code at the top of the tips. Php file:
The code is as follows: |
Copy code |
<? Php /* Template Name: tips */ ?> <? Php if (have_posts ():?> |
Add:
The code is as follows: |
Copy code |
<? Php query_posts ('showposts = 10 & cat = 1');?> |
// 1 indicates the Category id.
Finally, create a new page, such as the tips page. In the template selection area, select the newly created tips template and publish it. Refresh the homepage and click here to see the results.
If you want to display a document of the specified category on the homepage, refer to the following operations:
The code is as follows: |
Copy code |
// Only show articles under Category 14 <? Php while (have_posts (): the_post (); update_post_caches ($ posts);?> <? Phpif (in_category ('14'):?> <Div class = "post"> ...... Loop body structure code ...... </Div> <? Phpendif;?> <? Phpendwhile;?> |
Here, the number in in_category ('14 & prime;) represents the specified category ID, which can be found by editing the category URL.
In another case, only the articles under classification 14 are hidden. It is very simple to replace if (in_category ('14 & prime;) with if (! (In_category ('14 & prime.
WordPress obtains the number of specified articles by category.
Place the following PHP code in functions. php under the topic directory:
The code is as follows: |
Copy code |
Function wt_get_category_count ($ input = ''){ Global $ wpdb; If ($ input = ''){ $ Category = get_the_category (); Return $ category [0]-> category_count; } Elseif (is_numeric ($ input )){ $ SQL = "SELECT $ wpdb-> term_taxonomy.count FROM $ wpdb-> terms, $ wpdb-> term_taxonomy WHERE $ wpdb-> terms. term_id = $ wpdb-> term_taxonomy.term_id AND $ wpdb-> term_taxonomy.term_id = $ input "; Return $ wpdb-> get_var ($ SQL ); } Else { $ SQL = "SELECT $ wpdb-> term_taxonomy.count FROM $ wpdb-> terms, $ wpdb-> term_taxonomy WHERE $ wpdb-> terms. term_id = $ wpdb-> term_taxonomy.term_id AND $ wpdb-> terms. slug = '$ input '"; Return $ wpdb-> get_var ($ SQL ); } } |
You can call the function as needed. The Function provides three call methods:
1. If this function is called up in the main loop and no parameters are provided, the number of articles in the first category is returned:
The code is as follows: |
Copy code |
<? Php echo wt_get_category_count () ;?>
|