php – 如何以递归方式将特色图像应用于WordPress中的子页面?
发布时间:2020-12-13 22:52:21 所属栏目:PHP教程 来源:网络整理
导读:如果页面没有特色图像,则显示其父页面的特色图像. 如果父母没有特色图像,则从下一个更高级别获取它,依此类推,直到找到特色图像或达到最后一级. 在Wordpress中有解决方案吗? 解决方法 您可以使用这样的递归函数: function get_featured_recursive($post) {
如果页面没有特色图像,则显示其父页面的特色图像.
如果父母没有特色图像,则从下一个更高级别获取它,依此类推,直到找到特色图像或达到最后一级. 在Wordpress中有解决方案吗? 解决方法
您可以使用这样的递归函数:
function get_featured_recursive($post) { if (has_post_thumbnail( $post->ID ) ) { return $post->ID; } else if (!has_post_thumbnail($post->ID) && $post->post_parent != 0) { $parent = get_post($post->post_parent); if (has_post_thumbnail($parent->ID)){ return $parent->ID; } } else if(!has_post_thumbnail($parent->ID) && $parent->post_parent != 0){ get_featured_recursive(get_post($parent->post_parent)); } else if(!has_post_thumbnail($parent->ID) && $parent->post_parent == 0 ) { return null; } } 然后在您的模板中使用它来显示特色图像: < ?php $featured_image_post = get_featured_recursive($post); if ($featured_image_post != null) : $featured_image_src = wp_get_attachment_image_src(get_post_thumbnail_id($featured_image_post),'single-post-thumbnail'); ?> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |