Place the code bellow in your theme’s function.php file.
function readMore(){
$categoryDetails = get_the_category($post->ID);
$categoryID = $categoryDetails[0]->cat_ID;
switch ($categoryID):
case 1:
$readMoreText = 'Continue reading this tutorial';
break;
case 2:
$readMoreText = 'Watch screencast';
break;
default:
$readMoreText = 'Continue Reading';
endswitch;
echo '<p><a title="Permanent Link to '.get_the_title().'" href="'.get_permalink().'">'.$readMoreText.'</a></p>';
}
The number after the case is ID of a category. To easily find out IDs of your categories simply go to the categories page in wordpress admin and by hovering over the category names you will see a link like “…wp-admin/categories.php?action=edit&cat_ID=5“.
Now simply call the readMore() function wherever you want to show the link.
Visit the best themes marketplace with over 600 premium wordpress themes.













For simple lookups you use an array not a switch.
static $text = array(
‘default’ => ‘Continue Reading’,
1 => ‘Continue reading this tutorial’,
2 => ‘Watch screencast’,
);
if (!isset($text[$categoryID])) {
$categoryID = ‘default’;
}
$readMoreText = $array[$categoryID];
Secondly, it should return a string so you dont have to use output buffer to capture it. Its a good practice for the future.
function readMore()
{
//do stuff
return $readMore;
}
echo readMore();
@OIS – After you write a comment think about it before you hit the submit button. That way you won’t make 3 comments in 20 seconds. I’ll make them into one but in future take it slowly
1) Interesting.
2) Well for this specific wordpress function i think echo is the way to go.
P.S. Thanks for the downvote.
I usually downvote first, then come back to see if the article is changed or they have a good argument to convince me their way is good/better.
2) You should not assume how the output will be used. With this function you have to use output buffer to capture it if you want to use it for something else.
You see the functions get_the_title() and get_permalink() return a string and can be used in string concatenation. Its better to be consistent.
get_the_title() and get_permalink() are same as the_title() and the_permalink() except one uses return and the other uses echo. Inside of a theme the ones that echo are used, not the ones that return. Those that return are for specific cases as in this one where i need to get and not echo the value.
very useful post indeed