get_permalink() return duplicate URL during loop


I’m trying to show some posts in my slider; But this loop returns same link for all items

$recent_posts = get_posts(array('posts_per_page' => '6‍', 'meta_key' => 'post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC'));
foreach ($recent_posts as $post_item) : ?>
    <?php $author_id = $post->post_author; ?>
        <a href="<?= get_permalink($post) ?>" class="special-slide">
            <?php echo get_the_post_thumbnail($post_item, 'full'); 
<?php endforeach; ?>


You have a small mistake on the 4th line: you’re calling get_permalink($post) where $post is set for the current page, where you should have used get_permalink($post_item) – where $post_item is the variable from your loop.

(You’re also making the same mistake on the 3rd line when you’re trying to get the author_id as well)

Answered By – Hillel

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published