WordPress主題開發:WP_Query常用參數


常用參數 用途
調用文章或頁面
s 查詢和某個關鍵詞相關的所有的文章/頁面信息
p 文章或頁面id
post__in 多篇id
post__not_in 多篇id以外
post_type 查詢的信息類型,默認調用的是文章類型的,post為頁面類型
查詢某個作者發布的信息
author 按作者id
author_name 按作者別名
author__in 多個作者 'author__in' => array(1, 2)
author__not_in 除了某個作者以外'author__not_in' => array(2)
按分類目錄或標簽
 cat 按分類目錄編號
category_name  按分類目錄的別名
category__in 同時查詢多個目錄id
category__and  既在a目錄又在b目錄
tag 標簽別名
tag_id 按標簽編號
按關鍵字/信息類型/發布日期
 s  通過關鍵字搜索
  按時間
 year 按年
monthnum
  按日期
分頁參數
posts_per_page 每頁顯示信息數量
paged 分頁時顯示第幾頁,需設值獲取當前頁的分頁碼:get_query_var('paged')
排序
order 升序降序,默認為'DESC'降序,ASC升序
orderby 按什么排,比如按ID

 

例子:最新發表文章10篇

<ul>
                    <?php
                        $my_query=new WP_Query(
                            array(
                                'post_type'=>'post',
                                'posts_per_page'=>10,
                                'orderby'=>'date',
                                'order'=>'DESC'
                                 )   
                            );
                        if($my_query->have_posts()):while($my_query->have_posts()):$my_query->the_post();
                      ?>      
                        <li><a href="<?php the_permalink();?>"><?php the_title();?></a></li>
                     <?php
                        endwhile;
                        endif;
                    ?>
                    </ul>

 

注意:多個參數一起用是並列查詢的意思,

更多參考:

https://www.wpzhiku.com/all-wp_query-arguments-comments/

https://codex.wordpress.org/Class_Reference/WP_Query#Parameters


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM