Number of posts published on the current day and number of articles on the specified date when wordpress calls

Source: Internet
Author: User
Tags current time getdate

Number of posts published on the day when wordpress calls

When creating a wordpress topic, you need to call the number of articles published on the website of the current day. The blog post published the tutorial "number of articles for wordpress within a limited time period" to count the number of articles published within 24 hours, today's code counts the number of articles published on the day.

Implementation code:


<? Php
$ Today = getdate ();
$ Query = new WP_Query ('Year = '. $ today ["year"]. '& monthnum = '. $ today ["mon"]. '& day = '. $ today ["mday"]);
$ PostsNumber = $ query-> found_posts;
Echo $ postsNumber;
?>
Add the code to the position to be displayed.

Appendix: Number of articles of the day under the specified category called:


<? Php
$ Today = getdate ();
$ Query = new WP_Query ('Year = '. $ today ["year"]. '& monthnum = '. $ today ["mon"]. '& day = '. $ today ["mday"]. '& cat = 1 ');
$ PostsNumber = $ query-> found_posts;
Echo $ postsNumber;
?>

Tip: & cat = the next 1 is the category ID. How can wordpress get the category ID:

Method:

1. Log onto the blog background and click the classification directory tab under "Article;

2. Locate the related category directory and move the cursor to the category directory name;

3. A link address is displayed under the status bar at the bottom of the browser. _ ID = 1 is at the end of the address, and 1 is the ID of the category directory. Figure:

Note: In step 2, you do not need to click the directory name, but just place the mouse over it. In step 3, _ ID = 1 indicates the ID of the test directory on this site, the IDs of directories are different. Select the IDs based on your blog.


Wordpress: limited number of articles in a time period

It does not seem helpful to call the number of wordpress articles within a certain period of time. However, some websites may have special requirements to call the number of articles within a certain period of time, for example, for a time-sensitive Shopping Guide website, you can show the number of articles to tell visitors "XXX promotions today" and "XX special offers within three days, it also improves the user experience.

The code for wordpress to display the number of articles in a certain period of time is as follows:

Function num_posts ($ days = 1) {// $ days indicates a day;
Global $ wpdb;
$ Today = gmdate ('Y-m-d H: I: S', time () + 3600*8); // Obtain the current time
$ Daysago = date ("Y-m-d H: I: s", strtotime ($ today)-($ days * 24*60*60 )); // Today-$ days
$ Result = $ wpdb-> get_results ("select id from $ wpdb-> posts WHERE post_date BETWEEN '$ daysago' AND '$ today' AND post_status = 'Publish' AND post_type =' post 'Order BY post_date DESC ");
Foreach ($ result as $ Item ){
$ Post_ID [] = $ Item-> ID; // The published article ID, which is written into an array.
     }
$ Post_num = count ($ post_ID); // number of elements in the output array, number of article IDs, that is, number of published articles
$ Output. = '<a>'. $ post_num. '</a>'; // Number of output articles
Echo $ output;
}

Display by calling code:

<? Php echo num_posts ();?>

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.