how to create the pagination for customized portfolio template
First things first, that code shouldn't be in the template file; it should be in a preprocess function. The reasons for this have been documented at great length both on this site and the internet in general so I won't go into them here.

You might also find the l() function interesting, as well as theme_image()...API replacements for the custom HTML you're building up.

Other than that, the only thing you're missing is the code to actually render the pager; that would entail adding this to your preprocess function, after the query has been run:

$vars['pager'] = theme('pager');


And in the template file simply outputting it wherever you'd like it:

&lt;?php echo $pager; ?&gt;