Rewind_posts () function of the reset article loop in WordPress, wordpressgetposts_php tutorial

Source: Internet
Author: User

WordPress in the Reset article loop rewind_posts () function explained, wordpressgetposts


Sometimes, in index pages (home page, category article, label article, author article index ...) Advance into the WordPress article loop (loop) to get some of the information we want to get, but WP, a single page is generally only a one-time jump into the loop, that is, we will be the next time to draw information from the loop, we'll get the second log in the loop information, In order to solve this embarrassing situation, WordPress built a function, the rewind_posts () function is specifically used to reset the loop pointer.

Description description
Rewind the loop posts.
Resets the article loop.

Use
The function does not accept variables.

<?php rewind_posts ();?>

Instance
In this reference WordPress default theme Twenty eleven, author.php file 第15-55 line, and simplified.

<?php if (have_posts ()): The_post ();//Enter Loop  echo get_the_author ();//show article author, need to specify author ID rewind_posts () in out-of-loop use;// Reset Loop while (Have_posts ()): The_post (); Cycle begins   Get_template_part (' content ', Get_post_format ());

Summarize
Sometimes, WordPress has a function that must go into the loop in order to use, so we have to go into the loop in order to achieve some purposes, but if we use the loop in the next code, the loop will jump to the second position, so we have to use the rewind_posts () To reset the loop. Twenty eleven topic just give a very simple example, when to use or to see the theme of the author's environment, perhaps sometimes we want to cycle in a page two or more times the article, then change the function is more useful.

Ps:single_cat_title () function
Single_cat_title () function, which we seldom use in daily life, but this function will give us a lot of problems, such as the current page directory, tags, the function is not attached to the main WordPress loop, and can not be put into the main loop to use.

Describe
Gets the classification, label of the current page.

<?php Single_cat_title ($prefix, $display);?>
    • $prefix: Used to set what is displayed before the title.
    • $display: Used to set whether to display directly or return to a variable.

Instance
In this extract the WordPress 2011 default theme, the category.php file around the 18th line of the position of the code

<?phpprintf (' Category Archives:%s ', ' Twentyeleven '), '. Single_cat_title (', false '). '' );? >

Articles you may be interested in:

    • Use of PHP functions to get recent articles in WordPress development
    • Use the Wp_count_posts function in WordPress to count the number of articles
    • WordPress specific articles on search engine hide or only allow search engines to view
    • WordPress restricts non-admin users to comment only once after the article
    • Code examples for uninstalling plugins in WordPress and removing article type components
    • Ways to set default content in the WordPress article editor
    • Functions used in WordPress for obtaining information about articles and classifying links
    • Analysis of cyclic loop structure and usage of query article in WordPress
    • The method to get the author and classified information of the article in WordPress
    • Summary of relevant PHP functions for judging, adding and removing WordPress sticky articles

http://www.bkjia.com/PHPjc/1091103.html www.bkjia.com true http://www.bkjia.com/PHPjc/1091103.html techarticle WordPress in the Reset article loop rewind_posts () function explained, wordpressgetposts Sometimes, in the index page (home, category article, label article, author article index ...) ) to mention ...

  • 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.