1

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

<script>
function open_win()
{
window.open("<?php the_permalink(); ?>")
}
</script>

<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 Sep 28 '13 at 14:56
  • Javascript is client side (only in the browser), PHP is server side (can't be used in the browser). – Twifty Sep 28 '13 at 15:00
1

You could use the following:

<script>
function open_win( thePostURL )
{
window.open(thePostURL);
}
</script>
<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>
| improve this answer | |
  • 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 Sep 29 '13 at 17:27
  • Sorry, I'm not understanding, so what sort of issue are you having with the top code snippet then? – TomHarrigan Sep 29 '13 at 18:26
  • Sorry, I misread the code before. The top code snippet works like a charm. Thanks – ndru Sep 29 '13 at 21:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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