我正在尝试编写一个函数来检查是否使用了特色图像,或者以下函数是否捕获了第一张图像:
抓住帖子中的第一张图片
function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
$rooturi = get_theme_root_uri();
if(empty($first_img)){ //Defines a default image
$first_img = "$rooturi/reverie-master-child/img/default_image_bv_logo.jpg";
}
return $first_img;
}
到目前为止我的进步。尝试返回特色图像(如果已使用) - 如果它为空则使用上述功能捕捉第一张图像
function check_image_used() {
$featured_used = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
$content_image = catch_that_image();
if (empty($featured_used)) {
}
}
答案 0 :(得分:0)
function auto_featured_image() {
global $post;
if (!has_post_thumbnail($post->ID)) {
$attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
if ($attached_image) {
foreach ($attached_image as $attachment_id => $attachment) {
set_post_thumbnail($post->ID, $attachment_id);
}
}
}
}
// Use it temporary to generate all featured images
add_action('the_post', 'auto_featured_image');
// Used for new posts
add_action('save_post', 'auto_featured_image');
add_action('draft_to_publish', 'auto_featured_image');
add_action('new_to_publish', 'auto_featured_image');
add_action('pending_to_publish', 'auto_featured_image');
add_action('future_to_publish', 'auto_featured_image');