是的,有一种方法可以做到这一点。。。让我们看看has_post_thumbnail
功能本身:
function has_post_thumbnail( $post = null ) {
return (bool) get_post_thumbnail_id( $post );
}
正如您所看到的,它真正做的只是获取帖子缩略图ID并检查它是否存在。但这里没有过滤器。让我们更深入地了解:
function get_post_thumbnail_id( $post = null ) {
$post = get_post( $post );
if ( ! $post ) {
return \'\';
}
return get_post_meta( $post->ID, \'_thumbnail_id\', true );
}
仍然没有过滤器,但有希望:
function get_post_meta( $post_id, $key = \'\', $single = false ) {
return get_metadata(\'post\', $post_id, $key, $single);
}
最后
get_metadata
...
function get_metadata($meta_type, $object_id, $meta_key = \'\', $single = false) {
if ( ! $meta_type || ! is_numeric( $object_id ) ) {
return false;
}
$object_id = absint( $object_id );
if ( ! $object_id ) {
return false;
}
/**
* 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;
}
...
看来我们可以用
get_post_metadata
挂起以覆盖
has_post_thumbnail
后果你唯一需要记住的是,它会改变
get_post_thumbnail_id
而且
像这样的事情应该可以做到:
function my_override_has_post_thumbnail( $result, $object_id, $meta_key, $single ) {
if ( \'_thumbnail_id\' === $meta_key ) {
// perform your checks and return some ID if thumbnail exists
}
return $result;
}
add_filter( \'get_post_metadata\', \'my_override_has_post_thumbnail\', 10, 4 );