Creating Links to Posts and Categories in WordPress

A useful WordPress function which will links to posts dynamically so you don't have to embed a post URL into the page.

By Tim TrottWordPress • January 24, 2009
Creating Links to Posts and Categories in WordPress

We all know it is good to have links, posts, and categories throughout your site, but maintaining hard-coded links can be a pain, especially when things like permalink structure or domain name change. These two functions allow you to create flexible, dynamic links in your posts or pages.

So, you have a blog with hundreds of posts, each of which is interlinked. Then you decide to change your domain name, permalink structure or something else, and you must update all the posts again. Why not update them with a dynamic link and let WordPress generate your URL?

You will need RunPHP or similar for these to work inside posts and pages, but they are not needed if you use them in templates.

The first function will create a link to a post. You must specify a post ID to link to, you can optionally specify a title for the link (the text to show), and you can return the link as a string variable instead of printing it on the page. The post/page title will be used instead if no title is passed. These functions should be placed within functions.php in your themes folder.

php
function linkToPost($postid, $linkTitle = "", $echo = true)
{
  $linkurl = get_permalink($postid);
  
  if ($linkTitle == "")
    $linkTitle = get_the_title($postid);
    
  $link = '<a href="' . $linkurl . '" >' . $linkTitle . '</a>';
  
  if ($echo === true) 
    echo $link; 
  else 
    return $link;
}

The second function works the same way as the first. However, it will link to categories instead.

php
function linkToCat($catID, $linkTitle = "")
{
  $cat = get_category_link($catID);
  $link = '<a href="' . $cat . '" >' . $linkTitle . '</a>';
  echo $link;
}

Usage

Let's say you are writing a post and want to link to another post with an ID of 53 (ID numbers can be found when viewing/editing posts in the WordPress admin screens). To create a simple link to post 53, use the following:

php
<?php LinkToPost(53); ?>

This will be executed by RunPHP so that when the post is viewed, you will have a nice link that will always be correct.

To change the title to something other than the post title, use the functions like this:

php
<?php LinkToPost(53,"this is my new title"); ?>

And you can capture the output into a variable like this:

php
<?php $cleanLink = LinkToPost(53,"this is my title", false); ?>

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

My website and its content are free to use without the clutter of adverts, popups, marketing messages or anything else like that. If you enjoyed reading this article, or it helped you in some way, all I ask in return is you leave a comment below or share this page with your friends. Thank you.

There are no comments yet. Why not get the discussion started?

New comments for this post are currently closed.