php – 如何以递归方式将特色图像应用于WordPress中的子页面?

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 如何以递归方式将特色图像应用于WordPress中的子页面?脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
如果页面没有特色图像,则显示其父页面的特色图像.

如果父母没有特色图像,则从下一个更高级别@L_512_4@它,依此类推,直到找到特色图像或达到最后一级.

wordpress中有解决方案吗?

解决方法

@H_403_16@ 您可以使用这样的递归函数

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

脚本宝典总结

以上是脚本宝典为你收集整理的php – 如何以递归方式将特色图像应用于WordPress中的子页面?全部内容,希望文章能够帮你解决php – 如何以递归方式将特色图像应用于WordPress中的子页面?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。