Get_post and get_posts function used in WordPress development, wordpressget_post_php Tutorial

Source: Internet
Author: User

The use of Get_post and get_posts functions in WordPress Development is detailed, wordpressget_post


Get_post ()
The Get_post () function is seldom used in general theme production, but since we will be talking about get_posts () Later, we have to talk about this singular form first. The main function of this function is to return a specified article as an object or an array, so that we can use it later. Let's take a quick look at how he uses it.

Get_post () function description
WordPress function name is always so easy to understand, get_post () function as its table, that is, to obtain an article, a specified article is returned as an object or an array, so that we later use.

function uses

<?php Get_post ($post _id, $output);?>

$post _id variable to set the ID of the article to be obtained, it should be noted that this variable we can not directly give an actual value, that will be an error, we can only be called in the form of the following.

$pid = 158;get_post ($pid);//If it is like Get_post (158), then the call will be error-

$post _id, the default value is None, tested, this variable does not set the error, personally feel that the variable does not seem to have a default value.

$output variables are used to set the type of the returned data, with three types, objects, associative arrays, and arrays of values.
Objects: Object
Associative array: array_a
Array of values: Array_n
Default value: OBJECT

Working with instances

<?php$mypost_id = 158; $post _id_158 = Get_post ($mypost _id, array_a); $title = $post _id_158[' post_title '];? >

Get_posts
get_posts function, simple speaking is the plural of get_post new situation, but because it is a number of articles extracted, so the use of the method is slightly different, support a number of parameters to select the article needs to be extracted, in the CMS theme is often used, of course, if you have a deeper understanding of WordPress, You may also be using wp_query to replace the function, this is something, today the main introduction to Get_posts function.

get_posts function Explanation
This function belongs to the built-in function of WordPress, which is used to extract multiple assigned or random articles in WordPress.
The more you describe a simple function, the more complex it is to use, and the need to look closely at the back!

How to use

<?php $args = Array (  ' numberposts ' = =   5,  ' offset ' = =     0, '  category '  = ' = ' ' = '     = ' post_date ',  ' order ' = '      DESC ',  ' include '     = = ' Exclude ' = >,  ' meta_key '    =, ' Meta_value ', ' post_type ' and '    post ', '  post_ Mime_type ' = ' and '  post_parent '   , ' post_status ' and ' Publish '   ); $posts _array = get_posts ($args);?>

$args is the necessary variable for the function
Get_posts ($args) returns the variable of the array type.

Variable parameter details

<?php $args = Array (  //number of articles that need to be extracted ' numberposts ' = +//   with the   first article as the starting position  ' offset ' =     0,   the ID of the//category, multiple separated by commas, or pass number array, can specify multiple classification numbers.  //Most CMS uses the focus of this function.  ' category '    = +,   //sorting rules (note 1) ' ORDER by '  = ' post_date '     ,   //Ascending, descending ' ASC '--ascending (low to High) ' Desc '--descending (high to bottom)  ' order '      = ' DESC ',   //To display the article ID  ' include ' and/   /To exclude the ID  of the article ' Exclude ' +     ,   //custom field name  ' Meta_key ' +    ,  //custom field value, with the previous parameter, to select an article that displays values that match the custom field.  ' Meta_value ' +   ,   //post (log)-Default, page (pages),  //attachment (attachment), any--(All)  ' Post_type ' = =    ' post ',   //MIME type of article  ' post_mime_type '   = =//To display the article's parent ID  ' post_parent '   = >,   //Article Status  ' post_status '   = ' publish ');? >

Note:

    • ' Author '--Sort by author numeric number
    • ' Category '--Sort by category numeric number
    • ' Content '--Sort by content
    • ' Date '--Sort by creation date
    • ' ID '--Sort by article number
    • ' Menu_order '--Sort by menu order. Only pages are available.
    • ' Mime_type '--Sort by MIME type. Only attachments are available.
    • ' Modified '--sort by last modified time.
    • ' Name '--sort by stub.
    • ' Parent '--Sort by Parent ID
    • ' Password '--Sort by password
    • ' Rand '--any sort of result
    • ' Status '--Sort by status
    • ' title '--Sort by title
    • ' Type '--Sort by type

Instance
We've just talked about using arrays to pass arguments, and of course we can also use strings to pass arguments to the function, and here's a simple example.

<?php$posts_rand = get_posts (' Numberposts=3&orderby=rand ');? >

The above code is used to randomly get 3 articles in WordPress.

Summarize
In fact query_posts () and get_posts () function, accept most of the parameters, use the same structure of the database query statement, and can achieve the same purpose, but some of the topic author hint query_posts () may disturb the WordPress main loop, Therefore, it is not recommended to use here.
Get_posts use is not difficult, difficult to get the article after how to display in the page, here need to have a certain knowledge of PHP.
If you want to display the contents of the query outside of the article loop, take a look at Setup_postdata, a function that will give a lot of help to children's shoes that are used to the template tags.

Articles you may be interested in:

    • Use of PHP functions to get recent articles in WordPress development
    • Introduction to the use of PHP functions for custom menus in WordPress development
    • The PHP function used in WordPress to get the search form is parsed
    • Use the Wp_count_posts function in WordPress to count the number of articles
    • PHP functions for invoking comment templates and looping out comments in WordPress
    • Simple steps to add Google search to WordPress
    • Analysis of Post_class and Get_post_class functions in WordPress
    • Get_post_custom () function usage analysis in WordPress development
    • Installing Hana FLV player in WordPress with video player plugin
    • A detailed description of the use of classification function wp_list_categories in WordPress
    • The implementation of short code in the development of WordPress and the skill of using related functions

http://www.bkjia.com/PHPjc/1088782.html www.bkjia.com true http://www.bkjia.com/PHPjc/1088782.html techarticle Get_post and get_posts function used in WordPress development, Wordpressget_post Get_post () in the general theme of the production, get_post () function we generally rarely use, but because later will ...

  • 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.