The wordpress page displays the operation methods of specified articles.

Source: Internet
Author: User
Tags php file php code

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 () ;?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.