如何递归将特色图像应用于Wordpress中的子页面?
16lz
2021-01-22
If a page is not having featured image, display the featured image of its parent page.
如果页面没有特色图像,则显示其父页面的特色图像。
If again that parent is not having featured image, get it from the next higher level and so on till featured image is found or last level is reached.
如果父母再次没有特色图像,则从下一个更高级别获取它,依此类推,直到找到特色图像或达到最后一级。
Is there a solution for this in Wordpress?
在Wordpress中有解决方案吗?
2 个解决方案
#1
1
You can use a recursive function like this:
您可以使用这样的递归函数:
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;
}
}
Then in your template use this to display the featured image:
然后在您的模板中使用它来显示特色图像:
< ?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'); ?>
更多相关文章
- 如何在没有任何扩展名的php中保存图像
- php调整图像大小
- 从JSON字符串/数组中提取第一个图像
- 如何调整图像覆盖滑块
- 使用ASIHTTPRequest从iOS上传图像
- 在PHP中将PDF文档转换为预览图像,而无需安装Ghostscript
- 我需要PHP页面显示mysql数据库中的BLOB图像
- 如何使用mysql在php文件夹中快速更改图像名称?
- 视差滚动与图像仅通过徽标透明度