How do I link to posts, pages, and categories? PDF Print Email

There are two ways to make internal links from one content page to another in WordPress. One uses permalinks and one does not. The method that does not use permalinks works regardless of whether permalinks are enabled for your site.

Linking Without Using Permalinks

If you are not using Permalinks, how do you link to your pages, posts, and categories?

Using the numeric values found in the ID column of the Posts, Categories, and Pages Administration, you can create links as follows.

Posts

To link to a Post, find the ID of the target post on the Posts administration panel, and insert it in place of the '123' in this link:

<a href="/component/moofaq/?p=123">Post Title</a>

Categories

To link to a Category, find the ID of the target Category on the Categories administration panel, and insert it in place of the '7' in this link:

<a href="/component/moofaq/?cat=7">Category Title</a>

Pages

To link to a Page, find the ID of the target Page on the Pages administration panel, and insert it in place of the '42' in this link:

<a href="/component/moofaq/?page_id=42">Page title</a>

Date-based Archives

  • Year: <a href="/component/moofaq/?m=2006">2006</a>
  • Month: <a href="/component/moofaq/?m=200601">Jan 2006</a>
  • Day: <a href="/component/moofaq/?m=20060101">Jan 1, 2006</a>

Links On External Sites

If you are providing a link to your site from outside of your site, be sure to specify a full URL to the correct location:

<a href="http://example.com/index.php?p=123">post title</a>

If you have installed WordPress to a subfolder, such as wordpress, don't forget to add the folder to the link URL:

<a href="http://example.com/wordpress/index.php?p=123">post title</a>

Linking Using Permalinks

If you are using permalinks, you can use all of the above non-permalink techniques, which will work with permalinks enabled or not. If you have enabled permalinks, you have a few additional options for providing links that readers of your site will find a bit more user-friendly than the cryptic numbers.

The complexity of the URL depends on the complexity of your permalink configuration. If your permalink configuration (set on the Options > Permalinks Administration Panel) contains many Structure Tags, then the URL will be more difficult to construct.

Posts

For posts, replace each Structure Tag in your permalink structure with the data appropriate to a post to construct a URL for that post. For example, if the permalink structure is:

/index.php/archives/%year%/%monthnum%/%day%/%postname%/

Replacing the Structure Tags with appropriate values may produce a URL that looks like this:

<a href="/index.php/archives/2005/04/22/my-sample-post/">My Sample Post</a>

To obtain an accurate URL for a post it may be easier to navigate to the post within the WordPress blog and then copy the URL from one of the blog links that WordPress generates.

Review the information at Using Permalinks for more details on constructing URLs for individual posts.

Categories

To produce a link to a Category using permalinks, obtain the Category Base value from the Options > Permalinks Administration Panel, and append the category name to the end.

For example, to link to the category "testing" when the Category Base is "/index.php/categories", use the following link:

<a href="/index.php/categories/testing/">category link</a>

You can specify a link to a subcategory by using the subcategory directly (as above), or by specifying all parent categories before the category in the URL, like this:

<a href="/index.php/categories/parent_category/sub_category/">subcategory link</a>

Pages

Pages have a hierarchy like Categories, and can have parents. If a Page is at the root level of the hierarchy, you can specify just the Page's "page slug" after the static part of your permalink structure:

<a href="/index.php/a-test-page">a test page</a>

Once again, the best way to verify that this is the correct URL is to navigate to the target Page on the blog and compare the URL to the one you want to use in the link.

Date-based Archives

  • Year: <a href="/index.php/archives/2006">2006</a>
  • Month: <a href="/index.php/archives/2006/01/">Jan 2006</a>
  • Day: <a href="/index.php/archives/2006/01/01/">Jan 1, 2006</a>

Links on External Sites

Permalink structures should begin with a slash, meaning that they are anchored at the root of the site's URL. You should be able to prepent the protocol and server name to any link that begins with a slash to build a successful full URL.

For example, this category link:

<a href="/index.php/categories/parent_category/sub_category/">subcategory link</a>

Becomes this category link using a full URL:

<a href="http://example.com/index.php/categories/parent_category/sub_category/">subcategory link</a>

Combining Links with Template Tags

You can customize your links in the header, footer, or sidebar to be combinations of link types. This example features links to two categories, the main index page, a post, a static page, and uses the Pages template tag.

Note carefully that the wp_list_pages() template tag generates its own List Item (LI) so it doesn't need to be wrapped in a LI tag. This template tag is also set to list only the parent Pages and not their subPages or "children".

<ul id="linklist">
<li>
<?php _e('Check It Out'); ?>
<ul id="pageslist">
<li>
<a title="Home Page" href="/index.php">Home</a>
</li>
<li>
<a title="Blog" href="/component/moofaq/?cat=7">Blog</a>
</li>
<li>
<a title="Life Story" href="/component/moofaq/?p=12">My Life Story</a>
</li>
<?php wp_list_pages('exclude=4&depth=1&sort_column=menu_order&title_li='); ?>
<li>
<a title="Links and Resources" href="/component/moofaq/?cat=33">Links</a>
</li>
<li>
<a title="Site Map" href="/sitemap.php">Site Map</a>
</li>
</ul>
</li>
</ul>

Using such a customized list, you can also add CSS classes to change the look of each of the links, or style the entire section. It's up to you.

Absolute versus Relative Links

Absolute links define absolutely where to find the target of the link.
Relative links define the location of another document in relation to the current document.

Absolute Link Examples

Full URIs of the form http://example.com/wordpress/index.php are absolute links.

Absolute links can also point to your own server. When doing so, you may safely omit the http://domain.com prefix, and link to the target with a full path:

/wordpress/index.php

The leading slash means "At the very top of this domain is a directory named wordpress, and inside this directory is a file named index.php".

A document at

http://example.com/wordpress/index.php

contains a link of the form

/wordpress/index.php

The link above, when clicked, will take the viewer to

http://example.com/wordpress/index.php

Relative Link Examples

Relative links do not start with a slash:

wordpress/index.php

The lack of a leading slash means "Inside the current directory is a sub-directory named wordpress, and inside that directory is a file named index.php".

A document at

http://example.com/wordpress/index.php

contains a link of the form

wordpress/index.php

The link above, when clicked, will take the viewer to

http://example.com/wordpress/wordpress/index.php

Let us consider the case where in our blog where we are editing

http://example.org/blog/2009/01/04/nurds-on-the-loose

From it we can make links

  1. <a href="/../01/happy-new-year">New Years Announcement</a>
  2. <a href="/../../01/01/happy-new-year">New Years Announcement</a>
  3. <a href="/../../../2009/01/01/happy-new-year">New Year's Announcement</a>
  4. <a href="/../../../2008/12/25/merry-christmas">Christmas Announcement</a>

Note 1, 2, and 3 are all valid to achieve the same link. However with 4 there is no shortcut, as we must "climb" all the way into the previous year.

The links are all relative and we need not hardwire in any more knowledge than is necessary, which also will help portability if one day we export the blog elsewhere. (What happens if one day we however pick a different permalink structure via the administration panels is unknown...)

However, the above assumes we are always viewing a single post. If in fact we are viewing that same post in an archive, well, then all our assumptions of where we are now are wrong! So we see that however smart relative linking looks, it has a fatal flaw and cannot be chosen!

For more information on absolute and relative links, see the WebReference Tutorial on Absolute and Relative Links.

 

New Joomla Templates

  1. Social ConnectedSocial ConnectedName: Social ConnectedDescription: Social Connected is a new professional, easy to use Joomla template released by the Joomladesigns team which includes custom CSS style layouts for the Jomsocial and K2 Extensions. The Social Connected template includes the following features Three Jooml ...Owner: JoomlaDesignsTags: Computers, Communications, Business, Architecture
  2. ExtendExtendName: ExtendDescription: Extend is a professional easy to use Joomla template released by the Joomladesigns Team. The new Extend template supports the following features Includes Four different Joomla Templates Built-in colour picker to customize the template colour scheme Bui ...Owner: JoomlaDesignsTags: Business, Beauty, Architecture
  3. NgineNgineName: NgineDescription: Ngine is a new professional Joomla template from Joomladesigns.co.uk which supports a wide range of features including Five built-in Joomla templates Slide show Slide pop up boxes K2 CSS styles RTL support Six built in font styles Lots of module posi ...Owner: JoomlaDesignsTags: Software, Portal, News, Games
  4. Simply City 2Simply City 2Name: Simply City 2Description: Simply City is a professional, fast loading Joomla template which is easy to use and includes a wide range of features. The web design also supports CSS styles for the popular K2 extension. The Joomla template includes the following features 3 multi col ...Owner: JoomlaDesignsTags: Computers, Communications, Business
  5. Simply CitySimply CityName: Simply CityDescription: Simply City is a professional, fast loading Joomla template which is easy to use and includes a wide range of features. The web design also supports CSS styles for the popular K2 extension. The Joomla template includes the following features 3 different ...Owner: JoomlaDesignsTags: Electronics, Computers, Communications, Business
  6. Show more...

Search Directory

Explore Directory

Top Joomla Design Teams