Foros del Web » Creando para Internet » Sistemas de gestión de contenidos » WordPress »

[SOLUCIONADO] Llamar la imagen del post en el excerpt

Estas en el tema de Llamar la imagen del post en el excerpt en el foro de WordPress en Foros del Web. Buenas, estoy iniciándome en el wordpress y en mi pelea me he encontrado con algo que me supera... He buscado mucho por google y no ...
  #1 (permalink)  
Antiguo 26/03/2013, 11:28
 
Fecha de Ingreso: noviembre-2011
Ubicación: Sevilla
Mensajes: 48
Antigüedad: 12 años, 4 meses
Puntos: 1
Pregunta Llamar la imagen del post en el excerpt

Buenas, estoy iniciándome en el wordpress y en mi pelea me he encontrado con algo que me supera...

He buscado mucho por google y no doy con la clave, así que os explico a ver si podéis dar con la solución.

Tengo un loop de categorías en la que muestro resúmenes... Quisiera que la misma imágen del post se vea en el resumen.

Este es el loop...
Código PHP:
Ver original
  1. <?php $lastposts = get_posts('category_name=Noticias&numberposts=5');foreach($lastposts as $post) : setup_postdata($post); ?>
  2.                 <li>
  3.                 <p><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></p>
  4.                 <?php excerpt('20');?><a href="<?php the_permalink(); ?>" rel="bookmark"><div id="readmore">Ver detalles</div></a>
  5.                 </li>
  6.                 <?php endforeach; ?>

Quisiera incluir la imagen justo antes del resumen...

De modo que quede así
Código PHP:
Ver original
  1. <?php $lastposts = get_posts('category_name=Noticias&numberposts=5');foreach($lastposts as $post) : setup_postdata($post); ?>
  2.                 <li>
  3.                 <p><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?>
  4.  
  5. IMAGEN
  6.  
  7. </a></p>
  8.                 <?php excerpt('20');?><a href="<?php the_permalink(); ?>" rel="bookmark"><div id="readmore">Ver detalles</div></a>
  9.                 </li>
  10.                 <?php endforeach; ?>
¿Alguna idea?
  #2 (permalink)  
Antiguo 26/03/2013, 15:20
Avatar de zanguanga
Moderadora
 
Fecha de Ingreso: julio-2009
Ubicación: España
Mensajes: 1.686
Antigüedad: 14 años, 9 meses
Puntos: 429
Respuesta: Llamar la imagen del post en el excerpt

Usa the_post_thumbnail

Código PHP:
Ver original
  1. <?php if ( has_post_thumbnail()) : ?>
  2.                 <div class="alignleft" >
  3.                     <?php the_post_thumbnail('thumbnail', array('class' => 'alignleft')); ?>
  4.                 </div>
  5.             <?php endif; ?>

Aquí tienes la referencia: http://codex.wordpress.org/Function_...post_thumbnail
__________________
Mi blog personal | Mi G+
  #3 (permalink)  
Antiguo 30/03/2013, 12:57
 
Fecha de Ingreso: noviembre-2011
Ubicación: Sevilla
Mensajes: 48
Antigüedad: 12 años, 4 meses
Puntos: 1
Respuesta: Llamar la imagen del post en el excerpt

No funciona, creo que el fallo está en que no se genera el thumbnail (desde mi ignorancia...)

Me tiene loco, si elimino el excerpt sale la entrada entera y se ve la imagen bien pero todo el texto... T.T

He probado así;

Código PHP:
Ver original
  1. <?php $lastposts = get_posts('category_name=Noticias&numberposts=5');foreach($lastposts as $post) : setup_postdata($post); ?>
  2.                 <li>
  3.                 <p><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></p>
  4.                 <?php if ( has_post_thumbnail()) : ?>
  5.                     <div class="alignleft" >
  6.                         <?php the_post_thumbnail('thumbnail', array('class' => 'alignleft')); ?>
  7.                     </div>
  8.                     <?php endif; ?>
  9.                 <?php excerpt('20');?><a href="<?php the_permalink(); ?>" rel="bookmark"><div id="readmore">Ver detalles</div></a>
  10.                 </li>
  11.                 <?php endforeach; ?>

Y así;

Código PHP:
Ver original
  1. <?php $lastposts = get_posts('category_name=Noticias&numberposts=5');foreach($lastposts as $post) : setup_postdata($post); ?>
  2.                 <li>
  3.                 <p><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></p>
  4.                         <?php the_post_thumbnail('thumbnail'); ?>
  5.                 <?php excerpt('20');?><a href="<?php the_permalink(); ?>" rel="bookmark"><div id="readmore">Ver detalles</div></a>
  6.                 </li>
  7.                 <?php endforeach; ?>

¿Es posible que me falte algo?
¿Es posible que el fallo esté a la hora de crear la entrada? Cuando creo la entrada simplemente añado la imagen desde el panel "Añadir Objeto", pego la URL de la imagen y listo...

Gracias por la respuesta Zanguanga
  #4 (permalink)  
Antiguo 30/03/2013, 13:08
 
Fecha de Ingreso: noviembre-2011
Ubicación: Sevilla
Mensajes: 48
Antigüedad: 12 años, 4 meses
Puntos: 1
Respuesta: Llamar la imagen del post en el excerpt

Vale, está solucionado, aunque de aquella manera...

He utilizado una función externa, quizás haga que se cargue más lento pero de momento es la solución que tengo, así que si en un par de semanas no sale otra mejor daré el tema por solucionado!

Código PHP:
Ver original
  1. function catch_that_image() {
  2.       global $post, $posts;
  3.       $first_img = '';
  4.       ob_start();
  5.       ob_end_clean();
  6.       $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
  7.       $first_img = $matches [1] [0];
  8.    
  9.       if(empty($first_img)){ //Defines a default image
  10.         $first_img = "/images/default.jpg";
  11.       }
  12.       return $first_img;
  13.     }


Código PHP:
Ver original
  1. <?php $lastposts = get_posts('category_name=Noticias&numberposts=5');foreach($lastposts as $post) : setup_postdata($post); ?>
  2.                 <li>
  3.                 <p><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></p>
  4.                 <img src="<?php echo catch_that_image() ?>" />            
  5.                 <?php excerpt('20');?><a href="<?php the_permalink(); ?>" rel="bookmark"><div id="readmore">Ver detalles</div></a>
  6.                 </li>
  7.                 <?php endforeach; ?>


Aquí el post de la solución:

http://www.wprecipes.com/how-to-get-the-first-image-from-the-post-and-display-it

Última edición por Davi91; 30/03/2013 a las 13:11 Razón: No sé que me pasa que al guardar se añade el [noparse] y no acepta el link directo, tendréis que copiar y pegar...
  #5 (permalink)  
Antiguo 01/04/2013, 18:06
Avatar de zanguanga
Moderadora
 
Fecha de Ingreso: julio-2009
Ubicación: España
Mensajes: 1.686
Antigüedad: 14 años, 9 meses
Puntos: 429
Respuesta: Llamar la imagen del post en el excerpt

Cita:
Iniciado por Davi91 Ver Mensaje
No funciona, creo que el fallo está en que no se genera el thumbnail (desde mi ignorancia...)

Me tiene loco, si elimino el excerpt sale la entrada entera y se ve la imagen bien pero todo el texto... T.T

He probado así;

Código PHP:
Ver original
  1. <?php $lastposts = get_posts('category_name=Noticias&numberposts=5');foreach($lastposts as $post) : setup_postdata($post); ?>
  2.                 <li>
  3.                 <p><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></p>
  4.                 <?php if ( has_post_thumbnail()) : ?>
  5.                     <div class="alignleft" >
  6.                         <?php the_post_thumbnail('thumbnail', array('class' => 'alignleft')); ?>
  7.                     </div>
  8.                     <?php endif; ?>
  9.                 <?php excerpt('20');?><a href="<?php the_permalink(); ?>" rel="bookmark"><div id="readmore">Ver detalles</div></a>
  10.                 </li>
  11.                 <?php endforeach; ?>

Y así;

Código PHP:
Ver original
  1. <?php $lastposts = get_posts('category_name=Noticias&numberposts=5');foreach($lastposts as $post) : setup_postdata($post); ?>
  2.                 <li>
  3.                 <p><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></p>
  4.                         <?php the_post_thumbnail('thumbnail'); ?>
  5.                 <?php excerpt('20');?><a href="<?php the_permalink(); ?>" rel="bookmark"><div id="readmore">Ver detalles</div></a>
  6.                 </li>
  7.                 <?php endforeach; ?>

¿Es posible que me falte algo?
¿Es posible que el fallo esté a la hora de crear la entrada? Cuando creo la entrada simplemente añado la imagen desde el panel "Añadir Objeto", pego la URL de la imagen y listo...
No veo ningíun error. Quizá solo falta establecer la imagen destacada, al editar el post, en el panel de la derecha abajo.
__________________
Mi blog personal | Mi G+
  #6 (permalink)  
Antiguo 04/04/2013, 04:22
Avatar de Nekko
Colaborador
 
Fecha de Ingreso: marzo-2008
Ubicación: Buenos Aires
Mensajes: 3.141
Antigüedad: 16 años
Puntos: 656
Respuesta: Llamar la imagen del post en el excerpt

Generalmente hay que marcar la imagen como destacada para que sea utilizada en the_post_thumbnail. Podés hacer que se añada automáticamente la primera que encuentre con este código:

Código PHP:
Ver original
  1. function auto_post_thumbnail() {
  2.           global $post;
  3.           $already_has_thumb = has_post_thumbnail($post->ID);
  4.               if (!$already_has_thumb)  {
  5.               $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
  6.                           if ($attached_image) {
  7.                                 foreach ($attached_image as $attachment_id => $attachment) {
  8.                                 set_post_thumbnail($post->ID, $attachment_id);
  9.                                 }
  10.                            } else {
  11.                                 set_post_thumbnail($post->ID, 'ID_imagen_por_defecto');
  12.                            }
  13.                         }
  14.       }
  15.  //Final de la función
  16. add_action('the_post', 'auto_post_thumbnail');
  17. add_action('save_post', 'auto_post_thumbnail');
  18. add_action('draft_to_publish', 'auto_post_thumbnail');
  19. add_action('new_to_publish', 'auto_post_thumbnail');
  20. add_action('pending_to_publish', 'auto_post_thumbnail');
  21. add_action('future_to_publish', 'auto_post_thumbnail');
__________________
Taller para crear themes wordpress desde cero | Presupuestos para trabajos particulares vía MP

Etiquetas: php, post
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 03:53.