This article mainly introduces the use of the get_post_custom () function in WordPress development for parsing. the get_post_custom () function is used to obtain custom fields. For more information, see get_post_meta, the custom field used to return the article is worth a function, but the get_post_custom () function is easier to use. if you use it in a loop, you do not even need to set any parameters.
In fact, the basic implementation of the get_post_custom () function is similar to that of the get_post_meta () function ~
Use get_post_custom ()
get_post_custom($postid);
Only one parameter is accepted.
$ Postid document id;
Instance demo
if (have_posts()) : while (have_posts()) : the_post(); var_dump(get_post_custom()); endwhile; endif;
The output result is as follows: (if the following field is set)
array(4) {[“_edit_last”]=>array(1) {[0]=>string(1) “1”}[“_edit_lock”]=>array(1) {[0]=>string(12) “1342451729:1”}[“_thumbnail_id”]=>array(1) {[0]=>string(3) “228”}[“xzmeta”]=>array(2) {[0]=>string(3) “xz1”[1]=>string(3) “xz2”}}
Get_post_custom_values and get_post_custom_keys
Because custom fields are divided into key values and value values, sometimes we need to obtain these two values separately, therefore, the get_post_custom_values and get_post_custom_keys functions are derived from WordPress. As for the meaning, I have not found much significance, except for some usage when deleting custom fields in batches, I really didn't come up with anything to use, maybe it would be of great significance in a vast CMS topic.
Previously, I wrote the get_post_custom function and the get_post_meta function. I thought that there are not many related functions for custom fields, so I just sorted them out, simply write all the functions related to the custom field, excluding some basic implementation code of the function.
Get_post_custom_values is used to obtain the value of a specified custom field in the current article and return it in an array.
while (have_posts()) : the_post(); var_dump(get_post_custom_values(‘xzmeta')); endwhile; endif;
The following results are returned.
(If the custom field is set)
array(2) {[0]=>string(3) “xz1”[1]=>string(3) “xz2”}
Get_post_custom_keys is used to obtain the key values of all the custom fields in the current article.
if (have_posts()) : while (have_posts()) : the_post(); var_dump(get_post_custom_keys()); endwhile; endif;
The following results are obtained:
(If the custom field is set)
array(4) {[0]=>string(10) “_edit_last”[1]=>string(10) “_edit_lock”[2]=>string(13) “_thumbnail_id”[3]=>string(6) “xzmeta”}