gpt4 book ai didi

php - 如何递归地将特色图片应用于 Wordpress 中的子页面?

转载 作者:可可西里 更新时间:2023-10-31 23:40:36 25 4
gpt4 key购买 nike

如果页面没有特色图片,则显示其父页面的特色图片。

如果父级再次没有特色图片,则从下一个更高级别获取它,依此类推,直到找到特色图片或达到最后一个级别。

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

关于php - 如何递归地将特色图片应用于 Wordpress 中的子页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23285978/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com