¿Cómo puedo mostrar el cargo miniatura de la más reciente de un cierto tipo de correos, en la aplicación?

wordpress.stackexchange https://wordpress.stackexchange.com/questions/5052

  •  16-10-2019
  •  | 
  •  

Pregunta

Estoy usando la excelente PHP Code Widget plug-in, y la falta para crear un widget que muestra el mensaje de miniaturas (aka imagen destacada) para el cargo más reciente 'proyecto'. Aquí está el código que tengo hasta ahora, pero no está produciendo el resultado deseado. Esta realidad nada salidas.

<?php
global $post;
$args = array(
    'post_type' => 'project',
    'posts_per_page' => 1,
    ); 
$thumbnails = get_posts($args);
foreach ($thumbnails as $post)
{
    setup_postdata($post);
        ?>
        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'thumbnail' ); ?></a>
        <?php 
        }

¿Alguna sugerencia sobre lo que tengo que cambiar para que esto funcione?

¿Fue útil?

Solución

Moving here so comments don't turn into a mess.

Basically there are two things that can go wrong here:

  1. Fetching of posts. Check that $thumbnails gets correct post and only one. I am not sure if that is the issue, but get_posts() docs list numberposts argument instead of posts_per_page that is listed for query_posts(). That might make a difference.

  2. Messing with global variables. For this usage I wouldn't touch global $post at all, directly or with setup_postdata() either. Just use non-loop versions of functions that can fetch things you need by post id - get_permalink() and get_the_post_thumbnail().

Licenciado bajo: CC-BY-SA con atribución
scroll top