rewind_posts function Explanation of reset article loop in WordPress

Source: Internet
Author: User
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 '). '' );? >

The above describes the WordPress in the reset article cycle of the Rewind_posts function, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Related Article

    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.