I'd like to create a link that opens the selected post via javascript onclick in the loop.

If I use <a href="<?php the_permalink(); ?>More...</a> in the loop it opens the correct post.

But If I use

function open_win()
window.open("<?php the_permalink(); ?>")

<a href="#" onclick="open_win()">More...</a>

it just won't open the correct selected post. How can I make this work?

  • have you read the codex page for the_permalink?
    – Milo
    Commented Sep 28, 2013 at 14:56
  • Javascript is client side (only in the browser), PHP is server side (can't be used in the browser).
    – Twifty
    Commented Sep 28, 2013 at 15:00

1 Answer 1


You could use the following:

function open_win( thePostURL )
<a href="#" onclick="open_win( '<?php the_permalink(); ?>' )">More...</a>

It seems like a bit more work than it's worth though, the following would have the same result of the link opening in a new window:

<a href="<?php the_permalink(); ?>" target="_blank">More...</a>
  • I need to get the URL with the right post ID dynamically though. That's the problem. And I can't do it with a normal HTML link.
    – ndru
    Commented Sep 29, 2013 at 17:27
  • Sorry, I'm not understanding, so what sort of issue are you having with the top code snippet then? Commented Sep 29, 2013 at 18:26
  • Sorry, I misread the code before. The top code snippet works like a charm. Thanks
    – ndru
    Commented Sep 29, 2013 at 21:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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