#How to use the query posts function in wordpress?
query posts is a very easy-to-use function for calling articles. It can display a variety of articles of a specific range on the same page. The following is a detailed introduction to the usage of powerful query posts in WordPress. Friends who like it can refer to
Recommendation: "wordpress tutorial"
query posts is a very easy to use function to call articles, you can do Display multiple specific ranges of articles on the same page. For example, you can call a list of articles in different ranges such as a certain category, tag, date, and author. These article lists can greatly enrich the content of WordPress pages and benefit SEO. Second-hand scientists have sorted out the functions used by query posts to call articles. They are explained below.
First of all, the general way of writing query posts. Usually, the query is defined first, then the article looping code is added, and then the query is reset.
The code is as follows:
<?php //定義要顯示的文章范圍查詢 query_posts(); //文章回圈 if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <!–這邊是當(dāng)判斷符合時列出的文章清單,你可以用< ?php the_xxx(); >系列來顯示相關(guān)的文章資訊–> <?php endwhile; else: ?> <!–這邊是顯示抓無資料時要跑出來的錯誤訊息–> <?php endif; //重置查詢(這是為了避免之后的查詢資料因為上面這段查詢而混亂掉) wp_reset_query(); ?>
All the functions below are based on the above framework. Just enter the corresponding parameters in query_posts(); to display the range of articles you want.
1. Category Parameters (article classification parameters)
cat - Enter the category number to display the articles in the category
category_name - Enter the category name to display the category Articles within
category__and – Displays articles that are included in multiple categories at the same time (only enter the category number to identify)
category__in – Displays articles within this category, but does not include subcategories Articles (only the category number can be entered to identify)
category__not_in - Except for articles in a certain category, articles in other categories and sub-categories are displayed (only the category number can be entered to identify)
The code is as follows:
<?php //僅顯示分類編號為4的文章(包括子分類文章) query_posts(‘cat=4′); //僅顯示分類名稱為Codex的文章(包括子分類文章) query_posts(‘category_name=Codex ‘); //顯示多個分類內(nèi)的文章(包括各子分類文章) query_posts(‘cat=2,6,17,38′); //除了分類編號為3的文章(包括子分類文章),其他文章都顯示 query_posts(‘cat=-3′); //顯示同時有分類編號為2及6的文章 query_posts(array(‘category__and’ => array(2,6))); //顯示分類編號為6的文章(但不包括子分類文章) query_posts(array(‘category__in’ => array(6))); //除了分類編號為2及6的文章,子分類及其他分類文章都顯示 query_posts(array(‘category__not_in’ => array(2,6))); ?>
2. Tag Parameters
tag - Enter the tag name to display the articles containing the tag
tag_id - Enter the tag number to display the articles containing the tag Article
tag__and – Displays articles that match multiple specific tags (limited to enter tag number to identify)
tag__in – Displays articles that match one of the specific tags (limited to enter tag number to identify) Identification)
tag__not_in – Articles with this tag will not be displayed (limited to entering the tag number to identify)
tag_slug__and – Displays articles that contain multiple specific tags (limited to entering the tag name) to identify)
tag_slug__in – Display articles that match one of the specific tags (limited to enter the tag name to identify)
The code is as follows:
<?php //僅顯示標(biāo)簽帶有cooking的文章 query_posts(‘tag=cooking’); //僅顯示標(biāo)簽帶有bread或baking的文章 query_posts(‘tag=bread,baking’); //僅顯示標(biāo)簽帶有bread及baking及recipe的文章 query_posts(‘tag=bread+baking+recipe’); //僅顯示標(biāo)簽帶有編號37及47的文章 query_posts(array(‘tag__and’ => array(37,47)); //僅顯示標(biāo)簽帶有編號37或47的文章 query_posts(array(‘tag__in’ => array(37,47)); //僅顯示標(biāo)簽不帶有編號37或47的文章 query_posts(array(‘tag__not_in’ => array(37,47)); ?>
3, Author Parameters (author Parameters)
author - Enter the author number to display the articles published by the author
author_name - Enter the author name to display the articles published by the author
The code is as follows:
<?php //僅顯示作者編號為3的文章 query_posts(‘a(chǎn)uthor=3′); //僅顯示作者編號不為3的文章 query_posts(‘a(chǎn)uthor=-3′); //僅顯示作者名稱為Harriet的文章 query_posts(‘a(chǎn)uthor_name=Harriet’); ?>
4. Sticky Post Parameters (sticky post parameters)
The code is as follows:
<?php //僅顯示置頂文章 array(‘post__in’=>get_option(‘sticky_posts’)) //將文章的置頂屬性清除掉,以正常文章順序排序(例如發(fā)表日期)顯示出來 caller_get_posts=1 ?>
Displays articles, but does not display sticky articles.
The code is as follows:
<?php query_posts(array(“post__not_in” =>get_option(“sticky_posts”))); ?>
Display articles with category number 6, display 3 articles per page, clear the top attribute of articles under this category, and sort in the normal order of articles (for example publication date) is displayed.
The code is as follows:
<?php query_posts(‘cat=6&posts_per_page=3&caller_get_posts=1′); ?>
5. Post & Page Parameters (article & paging parameters)
The code is as follows:
<?php //顯示文章編號為27的文章 ‘p’ => 27 //顯示文章代稱為about-my-life的文章 ‘name’ => ‘a(chǎn)bout-my-life’ //顯示分頁編號為7的分頁 ‘page_id’ => 7 //顯示分頁代稱為about的分頁 ‘pagename’ => ‘a(chǎn)bout’ //當(dāng)文章超過5篇時就僅顯示5篇文章并且搭配換頁程式碼顯示換頁連結(jié),設(shè)為-1則不換頁全部顯示。 ‘posts_per_page’ => 5 //當(dāng)設(shè)定為6時就顯示6篇文章,設(shè)為-1則顯示范圍內(nèi)的全部文章。 ‘showposts’ => 6 //僅顯示文章編號為5,12,2,14,7的這5篇文章 ‘post__in’ => array(5,12,2,14,7) //僅顯示文章編號不為5,12,2,14,7的其他全部文章 ‘post__not_in’ => array(6,2,8) //顯示文章類型為分頁的文章,預(yù)設(shè)值為post (文章),可以使用的數(shù)值有attachment(媒體檔頁面), page(分頁), post(文章),或revision(修訂)。 ‘post_type’ => ‘page’ //顯示文章狀態(tài)為公開性質(zhì)的文章,可以使用的數(shù)值有pending(審核中), draft(草稿), future(排程), private(私人), trash(垃圾) 。 ‘post_status’ => ‘publish’ //顯示文章范圍內(nèi)的第93頁 ‘post_parent’ => 93 ?>
6. Time Parameters (time Parameters)
Display the list of articles published on December 20.
The code is as follows:
<?php query_posts(‘monthnum=12&day=20′); ?>
Displays the list of articles published this week.
The code is as follows:
<?php $week = date(‘W’); $year = date(‘Y’); query_posts(‘year=’ . $year .‘&w=’ .$week ); ?>
Displays a list of articles published in the last 30 days.
The code is as follows:
<pre name="code" class="php"><?php function filter_where($where = ”) { $where .= ” AND post_date > ‘” . date(‘Ym-d’, strtotime(‘-30 days’)) . “‘”; return $where; } add_filter(‘posts_where’, ‘filter_where’); query_posts($query_string); ?>
7. Orderby Parameters (arrangement order parameters)
<?php //依照發(fā)表作者排列 orderby=author //依照日期排列 orderby=date //依照標(biāo)題排列 orderby=title //依照最后編輯時間排列 orderby=modified //依照分頁順序排列(僅適用于分頁) orderby=menu_order // (不知道XD…) orderby=parent //依照文章編號排列 orderby=ID //隨機排列 orderby=rand //依照自訂欄位數(shù)值排列 orderby=meta_value //依照預(yù)設(shè)排列 orderby=none //依照回響數(shù)排列 orderby=comment_count ?>
8. Pagination Parameters (pagination parameters)
The code is as follows:
<?php //當(dāng)值設(shè)定true時則為不分頁顯示,直接顯示全部文章 nopaging=true //顯示每頁文章顯示10篇 posts_per_page=10 //頁數(shù),例如當(dāng)設(shè)定為6時則就表示跳到第6頁 paged=6 //排列順序,ASC為按時間順序排列文章,若是DESC則是反向顯示文章 order=ASC ?>
9. Combination application example
Displays articles with category number 3 and published in 2004.
The code is as follows:
<?php query_posts(‘cat=3&year=2004′); ?>
Displays articles with category numbers 1 and 3 and two articles per page, arranged in reverse order by title.
<?php query_posts(array(‘category__and’=>array(1,3),‘posts_per_page’=>2,‘orderby’=>title,‘order’=>DESC)); ?>
is only displayed on the homepage, and the article is published in the month with category number 13.
<?php if (is_home()) { query_posts($query_string . ‘&cat=13&monthnum=’ . date(‘n’,current_time(‘timestamp’))); } ?>
Display articles with category number 1 and tag apples.
<?php query_posts(‘cat=1&tag=apples’); ?>
The above is the detailed content of How to use the function query posts in wordpress. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

There are four ways to adjust the WordPress article list: use theme options, use plugins (such as Post Types Order, WP Post List, Boxy Stuff), use code (add settings in the functions.php file), or modify the WordPress database directly.

Web development design is a promising career field. However, this industry also faces many challenges. As more businesses and brands turn to the online marketplace, web developers have the opportunity to demonstrate their skills and succeed in their careers. However, as demand for web development continues to grow, the number of developers is also increasing, resulting in increasingly fierce competition. But it’s exciting that if you have the talent and will, you can always find new ways to create unique designs and ideas. As a web developer, you may need to keep looking for new tools and resources. These new tools and resources not only make your job more convenient, but also improve the quality of your work, thus helping you win more business and customers. The trends of web development are constantly changing.

Importing WordPress source code requires the following steps: Create a sub-theme for theme modification. Import the source code and overwrite the files in the sub-topic. Activate the sub-theme to make it effective. Test the changes to make sure everything works.

To build a website using WordPress hosting, you need to: select a reliable hosting provider. Buy a domain name. Set up a WordPress hosting account. Select a topic. Add pages and articles. Install the plug-in. Customize your website. Publish your website.

Do you want to connect your website to Yandex Webmaster Tools? Webmaster tools such as Google Search Console, Bing and Yandex can help you optimize your website, monitor traffic, manage robots.txt, check for website errors, and more. In this article, we will share how to add your WordPress website to the Yandex Webmaster Tool to monitor your search engine traffic. What is Yandex? Yandex is a popular search engine based in Russia, similar to Google and Bing. You can excel in Yandex

Do you want to know how to use cookies on your WordPress website? Cookies are useful tools for storing temporary information in users’ browsers. You can use this information to enhance the user experience through personalization and behavioral targeting. In this ultimate guide, we will show you how to set, get, and delete WordPresscookies like a professional. Note: This is an advanced tutorial. It requires you to be proficient in HTML, CSS, WordPress websites and PHP. What are cookies? Cookies are created and stored when users visit websites.

To create an account on WordPress, simply visit its website, select the registration option, fill in the registration form, and verify your email address. Other ways to register include using a Google account or Apple ID. The benefits of signing up include creating a website, gaining features, joining the community, and gaining support.

Do you need to fix HTTP image upload errors in WordPress? This error can be particularly frustrating when you create content in WordPress. This usually happens when you upload images or other files to your CMS using the built-in WordPress media library. In this article, we will show you how to easily fix HTTP image upload errors in WordPress. What is the reason for HTTP errors during WordPress media uploading? When you try to upload files to Wo using WordPress media uploader
