那么,对于一个文章,是如何来获取特色图像 Featured Image的,下面来看一下。在后台的文章编辑界面,特色图像显示在这个位置。
对应的后台代码是 wp-admin/includes/meta-boxes.php
1 2 3 4 5 6 7 8 9 10 11
/** * Display post thumbnail meta box. * * @since 2.9.0 * * @param WP_Post $post A post object. */ functionpost_thumbnail_meta_box($post) { $thumbnail_id = get_post_meta( $post->ID, '_thumbnail_id', true ); //获取特色图像对应的ID echo _wp_post_thumbnail_html( $thumbnail_id, $post->ID ); //输出HTML }
继续找 get_post_meta ,在wp-includes/post.php中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/** * Retrieve post meta field for a post. * * @since 1.5.0 * * @param int $post_id Post ID. * @param string $key Optional. The meta key to retrieve. By default, returns * data for all keys. Default empty. * @param bool $single Optional. Whether to return a single value. Default false. * @return mixed Will be an array if $single is false. Will be value of meta data * field if $single is true. */ functionget_post_meta($post_id, $key = '', $single = false) { return get_metadata('post', $post_id, $key, $single); }
/** * Retrieve metadata for the specified object. * * @since 2.9.0 * * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user) * @param int $object_id ID of the object metadata is for * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for * the specified object. * @param bool $single Optional, default is false. * If true, return only the first value of the specified meta_key. * This parameter has no effect if meta_key is not specified. * @return mixed Single metadata value, or array of values */ functionget_metadata($meta_type, $object_id, $meta_key = '', $single = false) { if ( ! $meta_type || ! is_numeric( $object_id ) ) { returnfalse; }
$object_id = absint( $object_id ); if ( ! $object_id ) { returnfalse; } //echo $object_id . ':' . $meta_type . '-' . $meta_key . '-' . $single . '<br />'; /** * Filters whether to retrieve metadata of a specific type. * * The dynamic portion of the hook, `$meta_type`, refers to the meta * object type (comment, post, or user). Returning a non-null value * will effectively short-circuit the function. * * @since 3.1.0 * * @param null|array|string $value The value get_metadata() should return - a single metadata value, * or an array of values. * @param int $object_id Object ID. * @param string $meta_key Meta key. * @param bool $single Whether to return only the first value of the specified $meta_key. */ $check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, $single ); if ( null !== $check ) { if ( $single && is_array( $check ) ) return$check[0]; else return$check; }
/** * Update the metadata cache for the specified objects. * * @since 2.9.0 * * @global wpdb $wpdb WordPress database abstraction object. * * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user) * @param int|array $object_ids Array or comma delimited list of object IDs to update cache for * @return array|false Metadata cache for the specified objects, or false on failure. */ functionupdate_meta_cache($meta_type, $object_ids) { global$wpdb;
// Get meta info $id_list = join( ',', $ids ); $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id'; echo"SELECT $column, meta_key, meta_value FROM $table WHERE $column IN ($id_list) ORDER BY $id_column ASC" . "<br />"; $meta_list = $wpdb->get_results( "SELECT $column, meta_key, meta_value FROM $table WHERE $column IN ($id_list) ORDER BY $id_column ASC", ARRAY_A );
// Force subkeys to be array type: if ( !isset($cache[$mpid]) || !is_array($cache[$mpid]) ) $cache[$mpid] = array(); if ( !isset($cache[$mpid][$mkey]) || !is_array($cache[$mpid][$mkey]) ) $cache[$mpid][$mkey] = array();
// Add a value to the current pid/key: $cache[$mpid][$mkey][] = $mval; } }