Post Loop not Returning Permalink

You have made the mistake in getting the post link in your code.

Missing

  1. <?php global $post; ?> – Initialization at the start of the loop

After setting up the post data your query becomes like normal Wp_Query so that you can get the permalink() in normal method itself using the_permalink() instead of get_permalink($post->id).

Since your posts is behaving in the normal method you need to make the following changes.

  1. Remove the get_permalink() from the code
  2. Remove the echo tag in roder to display the permalink

Replace your code with the current one.

Wherever you have used

<?php echo get_permalink($post->ID); ?>

Replace it with

<?php the_permalink(); ?>

And your final code will look like as follows.

<?php $posts = get_posts( "category=17,12,35,23,24,25,13,27,14,26,22,16,29,15,19,20,21&numberposts=3" ); ?>
<?php if( $posts ) : ?>
<?php global $post; ?>
<?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
<div class="post homepage">
    <a href="https://wordpress.stackexchange.com/questions/240856/<?php the_permalink(); ?>"><img src="<?php $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); echo $feat_image;?>" /></a>
    <h3 class="homepage-post-title"><a href="https://wordpress.stackexchange.com/questions/240856/<?php the_permalink(); ?>"><?php echo $post->post_title; ?></a></h3>
    <a class="read-more-link" href="https://wordpress.stackexchange.com/questions/240856/<?php the_permalink(); ?>">Read More</a>
</div>
<?php endforeach; ?>
<?php endif; ?>

Leave a Comment