如何给wordpress添加特色图片(免插件)
首先我们找到主题目录下的functions.php,将以下代码,放入到合适的位置即可,然后将 ($post->ID, ‘5118’) 里面的数字改为你需要显示图片的ID即可,查看图片的ID 大的方法:
点击媒体库—你需要链接的图片—点开之后,在浏览器的链接框的最后就可以就看到你所在图片的ID了。
如果本文对你有帮助,你可以分享给你的朋友。
// 自定义特色图
function wpforce_featured() {
global $post;
$already_has_thumb = has_post_thumbnail($post->ID);
if (!$already_has_thumb) {
$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);
}
} else {
set_post_thumbnail($post->ID, '5118');
}
}
} //end function
add_action('the_post', 'wpforce_featured');
add_action('save_post', 'wpforce_featured');
add_action('draft_to_publish', 'wpforce_featured');
add_action('new_to_publish', 'wpforce_featured');
add_action('pending_to_publish', 'wpforce_featured');
add_action('future_to_publish', 'wpforce_featured');
温馨提示 : 非特殊注明,否则均为©李联华的博客网原创文章,本站文章未经授权禁止任何形式转载;IP地址:18.118.0.203,归属地:俄亥俄州Dublin ,欢迎您的访问!
文章链接:https://www.ooize.com/how-to-add-featured-pictures-to-wordpress-plug-in-free.html
文章链接:https://www.ooize.com/how-to-add-featured-pictures-to-wordpress-plug-in-free.html
订阅
登录
0 评论