I want to know if the opened page is a category/tag page. From the JavaScript file, I can pass only the permalink of the page. Is there a way I can find the page type?


One thing you could do is set a variable in the archive.php like <script>var isArchive = true;</script>. Or in any template do if (is_archive()) { /* set the variable */ }.

And then in the JS file do if (typeof isArchive != 'undefined') { } but there are probably way more elegant solutions that I'm unaware of.

This also probably assumes you load your scripts in the footer now that I think about it.

edit: of course you could also conditionally load your JS only on the archive pages and woudln't need to test anything in the javascript itself.

  • I am aware of both of these solutions, but neither of them are so elegant to use in a premium theme. I want do the check via AJAX. Reasons: 1. I don't want to output script tags unnecessarily in the code. 2. I want to have a single JS file. – mstoic Apr 28 '15 at 13:22
  • Fair point, and I'm too interested in this if someone posts a decent solution :) Or how to check tag/category/post format what so ever. – jimihenrik Apr 28 '15 at 13:41

The only way I could accomplish this is by using the wp_localize_script function. The function is quite straightforward and lets you simply send anything from server side to your script.

This also outputs the variables inline, but this is the best WordPress allowd in this situation.

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.