【问题标题】:How to let wordpress automatically add a featured image?如何让wordpress自动添加特色图片?
【发布时间】: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');

【问题讨论】:

标签: php wordpress


【解决方案1】:

最终代码:

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');

【讨论】:

    猜你喜欢
    • 2012-01-16
    • 2012-11-30
    • 1970-01-01
    • 1970-01-01
    • 2012-02-25
    • 1970-01-01
    • 2018-04-06
    • 2019-07-31
    • 2014-02-16
    相关资源
    最近更新 更多