要求作者为帖子设置特色图片

Gar*_*ods 4 php wordpress image function featured

我已经定制了我的Wordpress网站设计,使用特色图片非常过分.这就是为什么我需要要求非管理员发布的所有帖子都要求设置特色图像.

这怎么可能?

Gar*_*ary 6

您需要在自己编写的自定义插件中挂钩发布操作.虽然这是要求一个标题,这应该让你开始,你只需要检查是否已分配特色图像.

add_action( 'pre_post_update', 'bawdp_dont_publish' );

function bawdp_dont_publish()
{
    global $post;
    if ( strlen( $post->title ) < 10 ) {
        wp_die( 'The title of your post have to be 10 or more !' );
    }
}
Run Code Online (Sandbox Code Playgroud)

查看(has_post_thumbnail( $post->ID ))以确定帖子是否具有精选图像.


小智 5

鉴于Gary的上述示例,我已将以下内容写入我的functions.php文件中:

function featured_image_requirement() {
     if(!has_post_thumbnail()) {
          wp_die( 'You forgot to set the featured image. Click the back button on your browser and set it.' ); 
     } 
}
add_action( 'pre_post_update', 'featured_image_requirement' );
Run Code Online (Sandbox Code Playgroud)

我更喜欢在插件中看到这个 - 有一个名为Mandatory Field但它不适用于预定的帖子.两者都不是真正雄辩的解决方案.