WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to use something like previous_post_link('%link','',TRUE ) and put the path into a javascript variable.

So using something like

var previous = "<?php previous_post_link( '%link','',TRUE ); ?>";

outputs :

<a href="http://www.website.com/photo/1085" rel="prev"></a>

but instead I only want the path output in that variable.


I like this function because it allows you to remain in category so I am hoping for a solution where I still can easily have that as an option.

share|improve this question
up vote 4 down vote accepted

Add following function in your functions.php file.

function custom_post_link($format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true) {
if ( $previous && is_attachment() )
    $post = & get_post($GLOBALS['post']->post_parent);
    $post = get_adjacent_post($in_same_cat, $excluded_categories, $previous);

if ( !$post )

$link = get_permalink($post);

$format = str_replace('%link', $link, $format);

$adjacent = $previous ? 'previous' : 'next';
echo apply_filters( "{$adjacent}_post_link", $format, $link );

and instead of calling previous_post_link('%link','',TRUE ) function call it as custom_post_link('%link','',TRUE ) and it will output the path as following

share|improve this answer
Thanks! That is just what I needed. – zac Mar 25 '12 at 6:25
You are always welcome. – Vinod Dalvi Mar 25 '12 at 15:30

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.