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

更多相关文章

  1. 如何在没有任何扩展名的php中保存图像
  2. php调整图像大小
  3. 从JSON字符串/数组中提取第一个图像
  4. 如何调整图像覆盖滑块
  5. 使用ASIHTTPRequest从iOS上传图像
  6. 在PHP中将PDF文档转换为预览图像,而无需安装Ghostscript
  7. 我需要PHP页面显示mysql数据库中的BLOB图像
  8. 如何使用mysql在php文件夹中快速更改图像名称?
  9. 视差滚动与图像仅通过徽标透明度

随机推荐

  1. 复数型是c语言的数据类型吗?
  2. Visual C++2010如何编写并运行C++程序
  3. go 为什么比php性能高
  4. aspx是什么文件?
  5. C语言中数组所占字节怎么算
  6. gin是什么意思?
  7. c语言中undeclared identifier是什么意思
  8. C++如何给二维数组初始化
  9. 什么是Go语言?Go语言的优缺点介绍
  10. 一个c程序的执行是从哪里开始到哪里结束