我刚刚建立了一个主题,在当前页面上寻找一个特色图片,如果找不到特色图片,它将继续通过家谱向后看,直到找到一个。然后它会打印出来。
我写了一个小脚本来实现这一点,但它似乎效率低下。必须有更好的方法来做这件事。可能是我错过的WP功能?这是代码,请仔细查看。
//get the ancestors
$familyTree = get_ancestors($post->ID,\'page\');
array_unshift( $familyTree, $post->ID ); //add the current page to the begining of the list
//loop through the family tree until you find a result or exhaust the array
$featuredImageFound = false;
$i = 0;
while (!$featuredImageFound && ($i < count($familyTree)) ) {
if(has_post_thumbnail( $familyTree[$i] )){
$featuredImage = get_the_post_thumbnail($familyTree[$i], \'full\');
$featuredImageFound = true;
}
$i++;
}
// if the page has a featured image then show it
echo ( $featuredImageFound ? $featuredImage : "" );