Home >Backend Development >PHP Tutorial >Analysis of get_post_custom() function usage in WordPress development, wordpressgetpost_PHP tutorial
is the same as get_post_meta(). The custom field used to return the article deserves a function, except get_post_custom() Functions are simpler to use and you don't even need to set any parameters if used in a loop.
In fact, the basic implementation of the get_post_custom() function is similar to get_post_meta()~
get_post_custom() uses
get_post_custom($postid);
Accepts only one parameter
$postid article id;
Example demonstration
if (have_posts()) : while (have_posts()) : the_post(); var_dump(get_post_custom()); endwhile; endif;
The output results are as follows: (if the following fields are 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 keys and values, sometimes we need to obtain these two values separately, so WordPress has derived two functions: get_post_custom_values and get_post_custom_keys. As for As for the meaning, I really haven’t found much significance. Except for its use when deleting custom fields in batches, I really can’t think of any place where it can be used. Maybe it will be very useful in a vast CMS theme. The importance of the crew.
I wrote about the get_post_custom function and get_post_meta function before. I thought privately that there are not many functions related to custom fields anyway, so I sorted it out and simply wrote down all the functions related to custom fields, excluding of course Some basic implementation codes of the function.
get_post_custom_values is used to get the value of the specified custom field of the current article and returns it in the form of an array.
while (have_posts()) : the_post(); var_dump(get_post_custom_values(‘xzmeta')); endwhile; endif;
The following results will be returned roughly
(if the custom field is set)
array(2) { [0]=> string(3) “xz1” [1]=> string(3) “xz2” }
get_post_custom_keys is used to get the key values of all custom fields in the current article.
if (have_posts()) : while (have_posts()) : the_post(); var_dump(get_post_custom_keys()); endwhile; endif;
You will roughly get the following results:
(If custom fields are set)
array(4) { [0]=> string(10) “_edit_last” [1]=> string(10) “_edit_lock” [2]=> string(13) “_thumbnail_id” [3]=> string(6) “xzmeta” }