Quantcast
Channel: Web Design 101 » WordPress
Viewing all articles
Browse latest Browse all 6

Get the URL of a post or page in WordPress

$
0
0

When you need the URL for any post or page within WordPress, your best friend is WordPress function the_permalink(). This function WHEN USED IN THE LOOP returns the URL of the page or post.  This function has not parameters, and spits out the URL – so you cannot save it to a variable name for later use.

Example use would be:

1
<a href="<?php the_permalink(); ?>">permalink</a>

If you need to get the URL of a page or post OUTSIDE OF THE LOOP then your next best friend is WordPress function get_permalink(). This function can also be used to store the value into a variable for later use and required one variable, which is the ID of the page/post you want.

Example use would be:

1
2
3
4
5
6
7
<?php

$pageURL = get_permalink($ID);

echo '<a href="'.$pageURL.'">Click me!</a>';

?>

Where $ID was obtained elsewhere either via a database query or knowing the physical page ID.

This is ideal when doing web design on your WordPress sites which need a little more sophisticated functionality and advanced steps outside the basic loop.


Viewing all articles
Browse latest Browse all 6

Trending Articles