【发布时间】:2014-06-24 22:32:03
【问题描述】:
我正在尝试让 wordpress 在新帖子中自动添加特色图片。
条件:
- 帖子类别 ID 必须为 3
- 每个作者/用户都有自己的形象。
- 图像(已上传)具有以下名称格式: [名字作者]-[姓氏作者].jpg。
例如;当 Steve Jobs 提交帖子时,提交的帖子类别 id 应为 3,为该帖子设置的特色图片应为 'steve-jobs.jpg'。
最终工作代码:
function set_featured_image_blog_post() {
global $post;
$already_has_thumb = has_post_thumbnail( $post->ID );
$post_category = get_the_category( $post->ID );
if ( !$already_has_thumb && $post_category[0]->cat_ID == 3 ) {
if( $post->post_author == 3 ){
set_post_thumbnail( $post->ID, 165 );
}
else if( $post->post_author == 4 ){
set_post_thumbnail( $post->ID, 166 );
}
}
}
add_action('the_post', 'set_featured_image_blog_post');
add_action('save_post', 'set_featured_image_blog_post');
add_action('draft_to_publish', 'set_featured_image_blog_post');
add_action('new_to_publish', 'set_featured_image_blog_post');
add_action('pending_to_publish', 'set_featured_image_blog_post');
add_action('future_to_publish', 'set_featured_image_blog_post');
【问题讨论】:
-
我怎么知道 thumbnail_id?