Welcome to the Webmaster Forum.
-
How can I put a PHP variable into JS?
How can I put a PHP variable into JS?
I have a javascript on my WP blog and I want to give one of it's variables the name of the current post's category. The JS code line looks like this:
my_keyword='dog food';
I need to figure out how to get the name of the category of the current post into the my_keyword variable. For instance, if the name of the category is stored in the PHP variable $this_cat I'd like to do something like:
my_keyword='<?php $this_cat ?>';
I know this is totally wrong (I've tried it), but is there a way to make the link between PHP and JS like this?
Also, I'm not having much luck getting the name of the current post's category in the first place. Here's my best guess:
<?php $postid = get_the_ID(); ?> (within the loop, then)
<?php $this_cat = get_the_category($postid)->cat_name; ?> (just before the JS, which is in the sidebar)
I hope I've explained what I'm trying to do clearly. I'm stoopid about PHP and JS so I'm rather out of my league with even this simple task. Any help would be greatly appreciated.
-
Hi......
PHP 5 has fixed the problem by setting the PHP configuration "allow_url_fopen_ to "off" in the configuration file. This allows webmasters to still use the include function, but they can't use absolute paths anymore. One way around this is to simply use relative paths instead, which are easier to type out and are a cinch to put into action.
Another method of using the include function in PHP 5 is to simply call the server's own base directory for calling files. This way the same syntax can be observed. The server variable for this base directory, "$_Server['document_root']," takes the place of the webmaster's domain name when including a file. Using this server variable, in effect, allows webmasters to still use absolute paths in their include functions. This is useful for bypassing changing all include functions to accommodate for relative paths.
Thanks
commercial fitness equipment
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
Powered by vBulletin™ Version 4.0.6
Copyright © 2010
Windows 8 Themes. All rights reserved.
All times are GMT. The time now is 07:58 PM.
Bookmarks