Wordpress: Wrong thumbnail and permalink being displayed

Asked

Viewed 107 times

2

I’m having trouble getting thumbnail (highlighted image) and permalink from the most recent post to be displayed on my blog home. 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 image displayed and the permalink I put in it are not from the last post but from an older post, from days ago. The date, title and excerpt from the most recent post appear normally.

I can’t figure out which part of the code I missed. Could you help me?

  • Should you not use the same instruction for thumbnail that you used for post_date and post_title guy: <?php echo $destacado->the_post_thumbnail(array(495, 368, true)); ?>

  • @marcosvinicius Unfortunately gave fatal error here in my tests...

1 answer

2


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

<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 it receives the parameter for the post ID, otherwise it will get from the Wordpress loop. And in your case the loop is customized and you’re not setting the global variable $post, then the best is to do so by picking up the ID.

  • Sorry for the lack of information. I will add the function code in the question. Anyway I edited the code following its orientation. I will check on the server if the problem has solved and give an answer soon.

  • 1

    <?php echo get_the_permalink($destacado->ID); ?> That would be right @Claudio Sanches?

  • 1

    @Claudio Sanches, thanks again! The code now displays the correct thumbnail.

  • 1

    @marcosvinicius I had not seen the the_permalink() thanks for telling us. @Giancarlo-silva I just put the permalink there too.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.