WordPress: Thumbnail and permalink wrong being displayed

2

I'm having trouble making the thumbnail and permalink of the most recent post appear on my blog's home page. I made the following code:

<?php
     $posts_noticias = get_noticias_posts();
     $destacado = array_shift($posts_noticias);
?>
<div id="noticia_destacada">
    <a href="<?php the_permalink() ?>">
        <?php the_post_thumbnail(array(495, 368, true)); ?>
        <span class="data_noticia"><?php echo $destacado->post_date ?></span>
        <h3 class="title_destacada"><?php echo $destacado->post_title ?></h3>
        <?php echo $destacado->post_excerpt ?>
    </a>
</div>

The code for the get_noticias_posts () function is as follows:

function get_noticias_posts($number = 5){
    $args = array(
    'posts_per_page'   => $number,
    'offset'           => 0,
    'category'         => get_cat_ID( 'noticias' ),
    'orderby'          => 'post_date',
    'order'            => 'DESC',
    'post_type'        => 'post',
    'post_status'      => 'publish',
    'suppress_filters' => true );

    $posts_array = get_posts( $args );

    return $posts_array;
}

However, the displayed image and the permalink I placed on it are not from the last post but from an older post, from days ago. The most recent post date, title, and excerpt appear normally.

I can not figure out where in the code I went wrong. Could you help me?

    
asked by anonymous 19.08.2014 / 16:15

1 answer

2

A little complicated to say, because it does not have many details and neither exactly what this get_noticias_posts() function does and how it returns the values. But looking at the values of the other things you printed seems to be returning a WP_Post object, so you can do this:

<div id="noticia_destacada">
    <a href="<?php echo get_the_permalink( $destacado->ID ); ?>">
        <?php echo get_the_post_thumbnail( $destacado->ID, array( 495, 368, true ) ); ?>
        <span class="data_noticia"><?php echo $destacado->post_date; ?></span>
        <h3 class="title_destacada"><?php echo $destacado->post_title; ?></h3>
        <?php echo $destacado->post_excerpt; ?>
    </a>
</div>

Just use the get_the_post_thumbnail() that he receives the parameter for the post ID, otherwise he will pick up of the WordPress loop. And in your case the loop is custom and you're not setting the global variable $post , so it's best to do so by grabbing the ID.

    
19.08.2014 / 16:57