Si una página no tiene una imagen destacada, muestre la imagen destacada de su página principal.
Si nuevamente ese padre no tiene una imagen destacada, obténgala del siguiente nivel superior y así sucesivamente hasta que se encuentre la imagen destacada o se alcance el último nivel.
¿Hay una solución para esto en WordPress?
nadeem khan
Puedes usar una función recursiva como esta:
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;
}
}
Luego, en su plantilla, use esto para mostrar la imagen destacada:
< ?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'); ?>
Add the below code in your functions.php and modify as per your need:
function get_featured_recursive($post) {
if (has_post_thumbnail( $post->ID ) ) {
return $post->ID;
} else if ($post->post_parent != 0) {
return get_featured_recursive(get_post($post->post_parent));
} else {
return null;
}
}
//In you display page:
div id="featured">
< ?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'); ?>
<div id="featured-bg"><img src="https://stackoverflow.com/questions/23285978/<?php echo$featured_image_src[0]; ?>" alt="<?php echo the_post_thumbnail_caption_from_id($featured_image_post); ?>" /> </div> </div> <div id="featured-caption">< ?php echo the_post_thumbnail_caption_from_id($featured_image_post); ?></div></div>
¿Ha sido útil esta solución?
Tu feedback nos ayuda a saber si la solución es correcta y está funcionando. De esta manera podemos revisar y corregir el contenido.