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 James

Hello there, I'm James. I studied Multimedia Design at university. I'm a designer at heart, though I love developing innovative and exciting solutions to web based products.I now specialise in front end web design and bespoke Wordpress powered websites.

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