0

Somewhat of a weird problem:

I have a post loop that display the three most recent posts from specific categories. It seems to work, except the post url is not being returned. It returns just the root url 'localhost:8888'. Any help would be appreciated!

    <?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 foreach( $posts as $post ) : setup_postdata( $post ); ?>
    <div class="post homepage">
        <a href="<?php echo get_permalink($post->ID); ?>"><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="<?php echo get_permalink($post->ID); ?>"><?php echo $post->post_title; ?></a></h3>
        <a class="read-more-link" href="<?php echo get_permalink($post->ID); ?>">Read More</a>
    </div>
    <?php endforeach; ?>
    <?php endif; ?>
1
  • I wouldn't reuse $post as a variable name, as it's a global variable. Consider using WP_Query too as an easier post loop with caching turned on by default – Tom J Nowell Sep 28 '16 at 17:25
1

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="<?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="<?php the_permalink(); ?>"><?php echo $post->post_title; ?></a></h3>
    <a class="read-more-link" href="<?php the_permalink(); ?>">Read More</a>
</div>
<?php endforeach; ?>
<?php endif; ?>
4
  • Weird- even with your updated code it's still not working. I tried to eliminate other factors and placed the code on a completely blank page - same result. Is there something tied to htaccess or some database setting? I tried changing my permalink settings and re-saving with no success. – Matt Fleming Sep 28 '16 at 20:07
  • in that case, the error has most likely nothing to do with the used code; what are your permalink settings? – Michael Sep 28 '16 at 22:25
  • My permalink settings is applied to Postname. And may i know what is your permalink Structure. – Naresh Kumar P Sep 29 '16 at 0:50
  • Why are you using the global $post? This code seems totally wrong to me, as the local $post variable is created by the "for" loop. – Mike Sep 8 '18 at 4:12
0

Had the similar issue, after creating category.php. My the_permalink() was not working, and it was returning just the same category instead going to the single post. It turned out, I had things modified in the permalinks menu of the admin panel. I had chosen "custom structure" and had /%groups% added, which rang me a bell that I might need to add /%postname% to it and it worked as /%groups%/%postname%. In your specific case scenario, you should use the /%post_id% at the end of that custom structure option.

-1

Using <?php global $post; ?> on top of your query should do the magic.

Even you can use <?php echo get_permalink(get_the_ID()); ?> instead of using <?php echo get_permalink($post->ID); ?>

Better and easy option is to use <?php the_permalink(); ?>

1
  • Wrong, you cannot use the global $post with the get_posts() function. You must use the functions with the post ID, because get_post() does not set the global variable $post. – Mike Sep 8 '18 at 4:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.