Get Individual WordPress Post Permalink

Follow @tristarweb

Hi guys, a really quick one for you today. I found the need to grab the permalink for the first post in a variety of custom post types. I used the following code, outside of the loop:

<?php $post = get_posts('numberposts=1&post_type=my-post-type' ); ?>
<a href="<?php echo get_permalink( $post[0]->ID ); ?>"><?php echo $post[0]->post_title; ?></a>

The above will grab the first post as an object array, and allow you to access information about it. You can use the offset parameter to get the 2nd, 3rd, 4th post, and so on.

Anyway, that’s about it!

Resources

get_post()
get_permalink()

About Gav Hoffman

Hi, I'm Gav and I try to make sure I am living life as much as possible and loving every minute of it. Designing and developing on a daily basis And lover of all things creative.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Please wrap all source code with [code][/code] tags.

Follow Tristar on Twitter