关注wordpress文章Loop循环中的$wp_query

在wordpress主题制作过程中,你可能会忽视LOOP循环中的全局参数$wp_query,它对于一些特殊情况下的开发有着非常重要的作用。我们可以利用$wp_query获取很多相关的信息,从而实现文章之外的查询和记录调用。

$wp_query记录的是对数据库进行一次检索得到的结构,它可以在query_posts()后面被打印,也可以在while(have_posts()):the_post();endwhile;中被打印,它的一些键值有着非常有效的作用。

举一个很简单的例子:我们可以利用它来获取在LOOP中当前显示的是第几篇文章,我们只需要用$current_post = $wp_query->current_post;即可获得(注意,$current_post的值从0开始自加1)。

我们也可以用$current_page = $wp_query->query_vars[‘paged’];得到当前是第几页。

我们可以在the_post()后使用print_r($wp_query)了解当前的$wp_query值。

它的基本构成类似是:(下面是我放在the_post()后的效果)

<br /> WP_Query Object<br /> (<br />     [query_vars] =&gt; Array<br />         (<br />             [cat] =&gt; -94,-117<br />             [paged] =&gt; 1<br />             [posts_per_page] =&gt; 2<br />             [post__in] =&gt; Array<br />                 (<br />                     [0] =&gt; 1352<br />                 )</p>
<p>            [caller_get_posts] =&gt; 1<br />             [orderby] =&gt; modified<br />             [error] =&gt;<br />             [m] =&gt; 0<br />             [p] =&gt; 0<br />             [post_parent] =&gt;<br />             [subpost] =&gt;<br />             [subpost_id] =&gt;<br />             [attachment] =&gt;<br />             [attachment_id] =&gt; 0<br />             [name] =&gt;<br />             [static] =&gt;<br />             [pagename] =&gt;<br />             [page_id] =&gt; 0<br />             [second] =&gt;<br />             [minute] =&gt;<br />             [hour] =&gt;<br />             [day] =&gt; 0<br />             [monthnum] =&gt; 0<br />             [year] =&gt; 0<br />             [w] =&gt; 0<br />             [category_name] =&gt;<br />             [tag] =&gt;<br />             [tag_id] =&gt;<br />             [author_name] =&gt;<br />             [feed] =&gt;<br />             [tb] =&gt;<br />             [comments_popup] =&gt;<br />             [meta_key] =&gt;<br />             [meta_value] =&gt;<br />             [preview] =&gt;<br />             [s] =&gt;<br />             [sentence] =&gt;<br />             [fields] =&gt;<br />             [category__in] =&gt; Array<br />                 (<br />                 )</p>
<p>            [category__not_in] =&gt; Array<br />                 (<br />                     [0] =&gt; 94<br />                     [1] =&gt; 117<br />                 )</p>
<p>            [category__and] =&gt; Array<br />                 (<br />                 )</p>
<p>            [post__not_in] =&gt; Array<br />                 (<br />                 )</p>
<p>            [tag__in] =&gt; Array<br />                 (<br />                 )</p>
<p>            [tag__not_in] =&gt; Array<br />                 (<br />                 )</p>
<p>            [tag__and] =&gt; Array<br />                 (<br />                 )</p>
<p>            [tag_slug__in] =&gt; Array<br />                 (<br />                 )</p>
<p>            [tag_slug__and] =&gt; Array<br />                 (<br />                 )</p>
<p>            [ignore_sticky_posts] =&gt; 1<br />             [suppress_filters] =&gt;<br />             [cache_results] =&gt; 1<br />             [update_post_term_cache] =&gt; 1<br />             [update_post_meta_cache] =&gt; 1<br />             [post_type] =&gt;<br />             [nopaging] =&gt;<br />             [comments_per_page] =&gt; 50<br />             [no_found_rows] =&gt;<br />             [order] =&gt; DESC<br />         )</p>
<p>    [tax_query] =&gt; WP_Tax_Query Object<br />         (<br />             [queries] =&gt; Array<br />                 (<br />                     [0] =&gt; Array<br />                         (<br />                             [taxonomy] =&gt; category<br />                             [terms] =&gt; Array<br />                                 (<br />                                     [0] =&gt; 94<br />                                     [1] =&gt; 117<br />                                 )</p>
<p>                            [include_children] =&gt;<br />                             [field] =&gt; term_id<br />                             [operator] =&gt; NOT IN<br />                         )</p>
<p>                )</p>
<p>            [relation] =&gt; AND<br />         )</p>
<p>    [meta_query] =&gt; WP_Meta_Query Object<br />         (<br />             [queries] =&gt; Array<br />                 (<br />                 )</p>
<p>            [relation] =&gt;<br />         )</p>
<p>    [post_count] =&gt; 1<br />     [current_post] =&gt; 0<br />     [in_the_loop] =&gt; 1<br />     [comment_count] =&gt; 0<br />     [current_comment] =&gt; -1<br />     [found_posts] =&gt; 1<br />     [max_num_pages] =&gt; 1<br />     [max_num_comment_pages] =&gt; 0<br />     [is_single] =&gt;<br />     [is_preview] =&gt;<br />     [is_page] =&gt;<br />     [is_archive] =&gt;<br />     [is_date] =&gt;<br />     [is_year] =&gt;<br />     [is_month] =&gt;<br />     [is_day] =&gt;<br />     [is_time] =&gt;<br />     [is_author] =&gt;<br />     [is_category] =&gt;<br />     [is_tag] =&gt;<br />     [is_tax] =&gt;<br />     [is_search] =&gt;<br />     [is_feed] =&gt;<br />     [is_comment_feed] =&gt;<br />     [is_trackback] =&gt;<br />     [is_home] =&gt; 1<br />     [is_404] =&gt;<br />     [is_comments_popup] =&gt;<br />     [is_paged] =&gt;<br />     [is_admin] =&gt;<br />     [is_attachment] =&gt;<br />     [is_singular] =&gt;<br />     [is_robots] =&gt;<br />     [is_posts_page] =&gt;<br />     [is_post_type_archive] =&gt;<br />     [query_vars_hash] =&gt; 062525d5faf49068e8e7abb314f2d7b6<br />     [query_vars_changed] =&gt;<br />     [thumbnails_cached] =&gt;<br />     [query] =&gt; Array<br />         (<br />             [cat] =&gt; -94,-117<br />             [paged] =&gt; 1<br />             [posts_per_page] =&gt; 2<br />             [post__in] =&gt; Array<br />                 (<br />                     [0] =&gt; 1352<br />                 )</p>
<p>            [caller_get_posts] =&gt; 1<br />             [orderby] =&gt; modified<br />         )</p>
<p>    [request] =&gt; SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts  WHERE 1=1  AND wp_posts.ID IN (1352) AND wp_posts.post_type = &#8216;post&#8217; AND (wp_posts.post_status = &#8216;publish&#8217;) GROUP BY wp_posts.ID ORDER BY wp_posts.post_modified DESC LIMIT 0, 2<br />     [posts] =&gt; Array<br />         (<br />             [0] =&gt; stdClass Object<br />                 (<br />                     [ID] =&gt; 1352<br />                     [post_author] =&gt; 1<br />                     [post_date] =&gt; 2012-08-22 22:09:32<br />                     [post_date_gmt] =&gt; 2012-08-22 14:09:32<br />                     [post_content] =&gt;<br />                     [post_title] =&gt;<br />                     [post_status] =&gt; publish<br />                     [comment_status] =&gt; open<br />                     [ping_status] =&gt; open<br />                     [post_password] =&gt;<br />                     [post_name] =&gt; %e6%b7%bb%e5%8a%a0%e4%b8%80%e4%b8%aa%e6%a0%87%e7%ad%be%e8%af%95%e8%af%95<br />                     [to_ping] =&gt;<br />                     [pinged] =&gt;<br />                     [post_modified] =&gt; 2012-08-27 16:09:52<br />                     [post_modified_gmt] =&gt; 2012-08-27 08:09:52<br />                     [post_content_filtered] =&gt;<br />                     [post_parent] =&gt; 0<br />                     [guid] =&gt; http://localhost/theme/?p=1352<br />                     [menu_order] =&gt; 0<br />                     [post_type] =&gt; post<br />                     [post_mime_type] =&gt;<br />                     [comment_count] =&gt; 10<br />                     [filter] =&gt; raw<br />                 )</p>
<p>        )</p>
<p>    [post] =&gt; stdClass Object<br />         (<br />             [ID] =&gt; 1352<br />             [post_author] =&gt; 1<br />             [post_date] =&gt; 2012-08-22 22:09:32<br />             [post_date_gmt] =&gt; 2012-08-22 14:09:32<br />             [post_content] =&gt;<br />             [post_status] =&gt; publish<br />             [comment_status] =&gt; open<br />             [ping_status] =&gt; open<br />             [post_password] =&gt;<br />             [post_name] =&gt; %e6%b7%bb%e5%8a%a0%e4%b8%80%e4%b8%aa%e6%a0%87%e7%ad%be%e8%af%95%e8%af%95<br />             [to_ping] =&gt;<br />             [pinged] =&gt;<br />             [post_modified] =&gt; 2012-08-27 16:09:52<br />             [post_modified_gmt] =&gt; 2012-08-27 08:09:52<br />             [post_content_filtered] =&gt;<br />             [post_parent] =&gt; 0<br />             [guid] =&gt; http://localhost/theme/?p=1352<br />             [menu_order] =&gt; 0<br />             [post_type] =&gt; post<br />             [post_mime_type] =&gt;<br />             [comment_count] =&gt; 10<br />             [filter] =&gt; raw<br />         )</p>
<p>)<br /> 

0

评论0

请先

没有账号? 注册  忘记密码?

社交账号快速登录