Method 1: Using the publish_post
hook* and media_sideload_image()
在这里,我们将从URL上传图像,将其附加到帖子,并在帖子发布时将其设置为特色图像。
*实际上,我们使用的是动态挂钩{$new_status}_{$post->post_type}
, 指的是转换post
将类型发布到publish
地位看见wp-includes/post.php
有关详细信息。
此示例代码在发布帖子类型时适用post
. 如果手动设置了特征图像,我们会进行检查,以便不会自动分配特征图像。您可以根据需要自由定制。
/**
* Download image, attach it to the current post, and assign it as featured image
* upon publishing the post.
*
* The dynamic portions of the hook name, `$new_status` and `$post->post_type`,
* refer to the new post status and post type, respectively.
*
* Please note: When this action is hooked using a particular post status (like
* \'publish\', as `publish_{$post->post_type}`), it will fire both when a post is
* first transitioned to that status from something else, as well as upon
* subsequent post updates (old and new status are both the same).
*
* @param int $post_id Post ID.
* @param WP_Post $post Post object.
*/
add_action( \'publish_post\', \'wpse_default_featured_image\', 10, 2 );
function wpse_default_featured_image( $post_id, $post ) {
// Bail if there is already a post thumbnail set.
$current_post_thumbnail = get_post_thumbnail_id( $post_id );
if ( \'\' !== $current_post_thumbnail ) {
return;
}
$url = \'https://www.cigarsofcuba.co.uk/acatalog/slide1.jpeg\';
$title = "The default featured image.";
$image = media_sideload_image( $url, $post_id, $title, \'id\' );
set_post_thumbnail( $post_id, $image );
}
Method 2: Applying a filter to the post thumbnail.
通过使用过滤器,我们可以“虚拟”设置帖子缩略图。此方法可以更轻松地动态更改所有帖子的帖子缩略图。
Filter 1: post_thumbnail_html
这个post_thumbnail_html
过滤器可用于覆盖帖子缩略图的HTML输出:
/**
* Filters the post thumbnail HTML.
*
* @param string $html The post thumbnail HTML.
* @param int $post_id The post ID.
* @param string $post_thumbnail_id The post thumbnail ID.
* @param string|array $size The post thumbnail size. Image size or array of width and height
* values (in that order). Default \'post-thumbnail\'.
* @param string $attr Query string of attributes.
*/
add_filter( \'post_thumbnail_html\', \'wpse_post_thumbnail_html\', 10, 5 );
function wpse_post_thumbnail_html( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
return \'<img src="https://www.cigarsofcuba.co.uk/acatalog/slide1.jpeg" alt="Have a cigar">\';
}
Filter 2: 使用
get_post_metadata
在
_thumbnail_id
元键。
这里有另一种方法可以用不同的ID覆盖每个帖子的缩略图ID。
我建议实现一个自定义设置或选项页面,允许上传/选择特殊的缩略图(本地、从URL或媒体库)。然后您可以使用get_post_metadata
筛选以修改与每个post缩略图的元键关联的值_thumbnail_id
:
/**
* Dynamic hook: "get_{$meta_type}_metadata"
*
* 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.
*
* @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.
*/
add_filter( \'get_post_metadata\', \'wpse_featured_image_id_override\', 100, 4 );
function wpse_featured_image_id_override( $value, $object_id, $meta_key, $single ) {
$thumbnail_id_key = \'_thumbnail_id\';
// Bail if this is not the correct meta key. Return the original value immediately.
if ( ! isset( $meta_key ) || $thumbnail_id_key !== $meta_key ) {
return $value;
}
// Add additional guard clauses if necessary... (check post type, etc.)
// Get the id for an image uploaded elsewhere.
// This could be pulled from the customizer or a plugin options page.
return 807; // Example ID
}