WordPress ÉÏ»ñÈ¡ÎÄÕÂ×iÖØÒªµÄ¾ÍÊÇÑ»£¨Loop£(C)£¬ÊÂʵÉÏÑ»¾ÍÊÇÈ¥Êý¾Ý¿a²eѯµ½ÏaÓ¦µÄÎÄÕ£¬È»ºoÔÝʱ´¢´aeµ½È«¾Ö±aÁ¿Ài±ß£¬ÐeÒªµÄʱºoһƪһƪµÄÊa³o³oÀ´£¬WordPress µÄÑ»Ée¼ÆµÄdz£ºÃ£¬Íe³ÉÒ»´ÎÑ»ÐeÒªÖ´ÐÐ 2000 ¶aÐдuÂe£¬¶øÄaÔÚʹÓÃÑ»µÄʱºo¿´µ½µÄÖ»ÊÇÒ»¸o¼oµ¥ while Ñ»*¼ÓÉϼ¸¸oº¯Êý£¬³oѧÕßÒ²ºÜºÃÀi½a¡£
<?php
if( have_posts() ):
while( have_posts() ):
the_post();
endwhile;
endif;
?>
Éϱ߾ÍÊÇÒ»¸oÆÕͨµÄÖ÷Ñ»½a¹¹£¬Ê×ÏÈÓà have_posts() º¯ÊýÅжÏÊÇñÓÐÎÄÕ£¬ÓÐÔoÓà while Ói¾aÑ»£¬ÔÚÑ»ÌaÀi±ßµ÷Óà the_posts() º¯Êý£¬the_posts() º¯ÊýÓÃÀ´¸aeËß WordPress ºËÐĵIJeѯÀa£¬ÒѾ²e¹ýһƪÎÄÕÂÁË£¬Ã¿µ÷ÓÃÒ»´Î the_posts()£¬Ñ»*Öеĵ±Ç°ÎÄÕ¾ͻa¸Ä±a¡£
µ±µ÷Óü¸´Î the_posts() Ö®ºo£¬ÎÄÕÂÒѾȫ²¿Ñ»Íe±Ï£¬¾ÍÈà have_posts() º¯Êýµ»Ø False À´½aÊøÑ»£¬ËuÒÔ£¬Ò»¸oÆÕͨµÄѻӦ¸ÃÊÇÕaÑuµÄ£º
<?php if( have_posts() ): ?>
<ul>
<?php while( have_posts() ):the_post();?>
<li <?php post_class(); ?>>
<?php the_title( '<h3>', '</h3>' ); ?>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
WP_QueryÀa
Ñ»ÖÐËuÓõ½µÄ½¨¶¼ÊÇÀ´×ÔÓÚWP_QueryÀa£¬ÕaÀiÔÙ¶ÔWP_QueryÀa×oһϿÆÆÕ£º
WP_Query ÀaµÄ¹¦ÄÜ°uÀ¨´¦Ài²eѯÌo¼þ¡¢È¥Êý¾Ý¿a»ñÈ¡ÎÄÕ¡¢ÕuÀiºÍ´¢´aeÎÄÕ¡¢ÎÄÕÂÑ»¡¢¸u¾ÝÎÄÕ²eѯÌo¼þÅжÏÒ³ÃaeÀaÐÍ¡£
ÊoÐÔ
$query
´«ÈeµÄ²eѯÌo¼þ¡£
$query_vars
´¦Ài $query µÃµ½µÄ¿ÉÒÔʹÓõIJeѯÌo¼þ¡£
$queried_object
¸u¾Ý²eѯÀaÐ͸ø³oµÄ²eѯ¹eÊo¡£±ÈÈçÈç¹uÊǸu¾ÝÖÀa²eѯ£¬ÄÇôµ»ØÒ»¸o°uº¬ÖÀaÐÅÏ¢µÄÀa£»Èç¹uÊDZeÇ(C)Ò³Ôoµ»Ø°uº¬±eÇ(C)ÐÅÏ¢µÄÀa£»×÷ÕßÒ³µÈÒÔ´ËÀaÍÆ¡£
$queried_object_id
µ»Ø $queried_object µÄ ID.±ÈÈçÖÀa¾ÍÊÇ*ÖÀa ID£»±eÇ(C)¾ÍÊDZeÇ(C) ID£»ÒÔ´ËÀaÍÆ¡£
$posts
´ÓÊý¾Ý¿aÖвeѯµ½µÄÎÄÕºÍÎÄÕÂÐÅÏ¢±»ÒÔÊý×eµÄÐÎʽ´¢´aeÔÚÕa¸o±aÁ¿Ài£¬Ã¿ÆªÎÄÕÂÊÇÒ»¸o´Ó WP_Post ÀaʵÀý»¯µÄ¶ÔÏo¡£
$post_count
µ±Ç°ÎÄÕÂÊýÁ¿£¬»»¾a»°Ëµ¾ÍÊÇ $posts ±aÁ¿Ài´ae´¢Á˼¸ÆªÎÄÕ¡£
$found_posts
ͳ¼ÆÈç¹u²»ÆoÓÃ*ÖÒ³»aÓжaÉÙÎÄÕ¡£
$max_num_pages
×ÜÒ³Êý£¬µ±Ç°ÎÄÕ±»*ֳɼ¸Ò³¡£
$current_post
µ±Ç°ÎÄÕµÄË÷ÒýÖµ¡£ÔÚÑ»ÖУ¬¿ªÊ¼Ñ» $current_post Ϊ ¨C1£¬Ã¿Ñ»Ò»´ÎÔo¼Ó 1£¬Ëu±iʾµ±Ç°Ñ»µ½µÄÎÄÕÂÔÚ $posts ±aÁ¿£¨Êý×e£(C)ÖеÄË÷ÒýÖµ¡£
$post
Ñ»*Öе±Ç°µÄÎÄÕ£¬ÊÇÒ»¸o°uº¬ÎÄÕºÍÎÄÕÂÐÅϢͨ¹ý WP_Post ÀaʵÀý»¯µÄ¶ÔÏo¡£
$is_single, $is_page, $is_archive, $is_preview, $is_date, $is_year, $is_month, $is_time, $is_author, $is_category, $is_tag, $is_tax, $is_search, $is_feed, $is_comment_feed, $is_trackback, $is_home, $is_404, $is_comments_popup, $is_admin, $is_attachment, $is_singular, $is_robots, $is_posts_page, $is_paged
¸u¾Ý²eѯÌo¼þÅжϵ±Ç°ÊÇʲôҳÃae£¬È»ºo´¢´aeÔÚÐ(C)±aÁ¿Ài£¬ÏeÇe¿ÉÒԲο¼ ¡¶ WordPress Ò³ÃaeÅжϺ¯Êý ¡*¡£
½¨
init()
³oʼ»¯¶ÔÏo£¬ËuÓÐÊoÐÔÉeÖÃΪ null¡¢-1¡¢0 »o False£¬É¾³ýÒ»Ð(C)ÊoÐÔ¡£
parse_query( $query )
½aÎo²¢±£´ae²eѯ×Ö*u´®¡£
parse_query_vars()
ÔÚ֮ǰµÄ WordPress °ae±¾ÖÐºÍ parse_query() Ò»ÑuµÄ¹¦ÄÜ£¬ÏÖÔÚÖ±½Óµ÷Óà parse_query() ¾ÍºÃÁË¡£
get( $query_var )
¸u¾ÝÃu×Ö»ñÈ¡Ò»¸o²eѯÌo¼þ¡£
set( $query_var, $value )
ÉeÖÃÒ»¸o²eѯÌo¼þ¡£
get_posts()
¸u¾ÝÏÖÓеIJeѯÌo¼þÈ¥Êý¾Ý¿a»ñÈ¡ÎÄÕ£¬²¢±£´aeÔÚ $posts ÊoÐÔÀi£¬Í¬Ê±ÉeÖúà $post_count ÊoÐÔ¡£´Ëº¯ÊýµÄ*µ»ØÖµ¾ÍÊÇ´ae´¢ÎÄÕ嵀 $posts ÊoÐÔ¡£
next_post()
£¨Ñ»¿ØÖÆ£(C)½« $current_post ¼Ó 1£¬Ò²¾ÍÊǰѵ±Ç°ÎÄÕÂÏoÇ°Íƽøһƪ£¬Í¬Ê±°ÑÍƽøºoµÄµ±Ç°ÎÄÕÂÌi¼Óµ½ $post ÊoÐÔÀi£¬È»ºoµ»Ø $post ÊoÐÔ¡£
the_post()
£¨Ñ»*¿ØÖÆ£(C)½øÈeÏÂһƪÎÄÕ£¬µ÷Óà next_post() º¯Êý£¬È»ºo°ÑÍƽøºoµÄµ±Ç°ÎÄÕµ¼Èeµ½È«¾Ö±aÁ¿Ài±ß¡£
have_posts()
£¨Ñ»¿ØÖÆ£(C)ÅжÏÑ»ÊÇñÍe³É£¬Èç¹uËuÓÐÎÄÕ¶¼Ñ»Íe±Ï»oÕ߸u±¾¾ÍûÓÐÎÄÕÂÔo*µ»Ø False.
rewind_posts()
£¨Ñ»¿ØÖÆ£(C)Ìøµ½Ñ»¿ªÍ*¡£Ïaµ±ÓÚÖØÖà $current_post ÊoÐÔºÍ $post ÊoÐÔ¡£
query( $query )
²eѯÎÄÕ£¬°uÀ¨µ÷Óà init() ½¨¡¢parse_query() ½¨ºÍ get_posts() ½¨£¬²¢µ»Ø get_posts() ½¨µÄµ»ØÖµ£¬Óô˺¯Êý¿ÉÒÔÍe³ÉÒ»¸oÎÄÕ²eѯµÄÍeÕu²Ù×÷¡£
get_queried_object()
Éu³É $queried_object ÊoÐÔ£¬Èç¹uÒѾ´aeÔÚÔoÖ±½Óµ»Ø $queried_object ÊoÐÔ£¬ËuÒÔ˵£¬ÒoΪĬÈÏ $queried_object ÊDz»´aeÔڵģ¬ÏeÒªµ÷Óà $queried_object ÊoÐÔÒ»¶¨ÒªÖ±½ÓʹÓà get_queried_object() ½*¨¡£
get_queried_object_id()
Éu³É $queried_object_id ÊoÐÔ£¬Èç¹uÒѾ´aeÔÚÔoÖ±½Óµ»Ø $queried_object_id ÊoÐÔ¡£ÀaËÆ get_queried_object() ½*¨¡£
WP_Query( $query = '' ) £¬ __constructor( $query = '' )
WP_Query ÀaµÄ¹¹Ôiº¯Êý£¬ÊµÀý»¯ÀaµÄʱºo×Ô¼ºµ÷Óã¬Èç¹uÄa´«ÈeÒ»¸o²eѯÌo¼þ£¬Ëu»a×Ô¶¯µ÷Óà query() ½¨°iÄa²eѯÎÄÕ¡£