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.
I'm having a hard time following the second part of the question, but to put a PHP variable in javascript I would have PHP output the entire <script> tag like so: <?php $keyword = 'Your Keyword'; echo ' <script type="text/javascript" language="javascript"> var my_keyword = \''.$keyword.'\'; </script>'; ?> PHP: PHP is relatively easy to put into javascript - putting javascript in PHP is impossible though. Explain more about the $postid part and I'll give you a hand
Thanks for the tip... I'll have to study it a bit - especially this part: \''.$keyword.'\' Code (markup): Why is the first slash outside the quotes, but the second one is inside? What I was saying was that I need to determine the category name of the current post. That's it. I confused things by putting my own lame attempts at coding into the post, but basically, if you have a wordpress blog and you need to get the name of the category of that post, how do you do it? How does one get the name of the category of the current post into the $keyword variable? Thanks again..
I tried the code you posted and I got an error: Line 64 is the one that says < echo ' > Here is the actual code I'm working with: <?php $keyword = 'weight loss'; echo ' <script type="text/javascript"> hopfeed_template=""; hopfeed_align='LEFT'; hopfeed_type='IFRAME'; hopfeed_affiliate_tid=''; hopfeed_affiliate='affiliate'; hopfeed_fill_slots='true'; hopfeed_height=600; hopfeed_width=200; hopfeed_cellpadding=5; hopfeed_rows=8; hopfeed_cols=1; hopfeed_font='Verdana, Arial, Helvetica, Sans Serif'; hopfeed_font_size='9pt'; hopfeed_font_color='#000000'; hopfeed_border_color='#FFFFFF'; hopfeed_link_font_color='#3300FF'; hopfeed_link_font_hover_color='#3300FF'; hopfeed_background_color='#FFFFFF'; hopfeed_keywords=\''.$keyword.'\'; hopfeed_path='http://affiliate.hopfeed.com'; hopfeed_link_target='_blank'; </script>'; ?> <script type="text/javascript" src='http://affiliate.hopfeed.com/script/hopfeed.js'></script> Code (markup): Perhaps it will help to see the whole script.
<?php $keyword = 'weight loss'; echo " <script type=\"text/javascript\"> hopfeed_template=''; hopfeed_align='LEFT'; hopfeed_type='IFRAME'; hopfeed_affiliate_tid=''; hopfeed_affiliate='affiliate'; hopfeed_fill_slots='true'; hopfeed_height=600; hopfeed_width=200; hopfeed_cellpadding=5; hopfeed_rows=8; hopfeed_cols=1; hopfeed_font='Verdana, Arial, Helvetica, Sans Serif'; hopfeed_font_size='9pt'; hopfeed_font_color='#000000'; hopfeed_border_color='#FFFFFF'; hopfeed_link_font_color='#3300FF'; hopfeed_link_font_hover_color='#3300FF'; hopfeed_background_color='#FFFFFF'; hopfeed_keywords='$keyword'; hopfeed_path='http://affiliate.hopfeed.com'; hopfeed_link_target='_blank'; </script>"; ?> <script type="text/javascript" src='http://affiliate.hopfeed.com/script/hopfeed.js'></script> PHP: you need to escape the quote.
Excellent! Thank you guys so much for the help! With what you showed me I was able to get the JS/PHP connection working, and by digging around and scratching my head and trying a lot of stuff I managed to figure out how to make it dynamically update to reflect the category of the currently displayed post. Just exactly what I was wanting! Here's what I got: <?php $postid = get_the_ID(); $category = get_the_category($postid); $current_category = $category[0]->cat_name; echo " <script type=\"text/javascript\"> hopfeed_template=''; hopfeed_align='LEFT'; hopfeed_type='IFRAME'; hopfeed_affiliate_tid=''; hopfeed_affiliate='affiliate'; hopfeed_fill_slots='true'; hopfeed_height=600; hopfeed_width=200; hopfeed_cellpadding=5; hopfeed_rows=8; hopfeed_cols=1; hopfeed_font='Verdana, Arial, Helvetica, Sans Serif'; hopfeed_font_size='9pt'; hopfeed_font_color='#000000'; hopfeed_border_color='#FFFFFF'; hopfeed_link_font_color='#3300FF'; hopfeed_link_font_hover_color='#3300FF'; hopfeed_background_color='#FFFFFF'; hopfeed_keywords='$current_category'; hopfeed_path='http://affiliate.hopfeed.com'; hopfeed_link_target='_blank'; </script>"; ?> <script type="text/javascript" src='http://affiliate.hopfeed.com/script/hopfeed.js'></script> Code (markup): Now I can display CB hopfeeds that are context-sensitive - just like adsense! Rockin!! Thanks again for all your help!