1

I am trying to put Jquery into a PHP file. I need to put a twitter feed that works perfectly when i put into a HTML file but I cant get it to work in my homepage.php file.

It is from this tutorial.

I have created the seperate javascript file and tried to echo the script but I am having no luck.

Here is the php file:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.twitter.feed.js"></script>

<script type="text/JavaScript">
    $(document).ready(function() {
        $('#twitter-feed').dcTwitterFeed({
            id: '#thepaulmac',
            tweetId: 'thepaulmac',
            retweets: false,
            replies: false,
            avatar: false,
            results: 20,
            images: 'small'
        });
    }); 

</script>


<?php get_header(); ?>
            <div id="twitter-feed"></div>

Does anyone know what I would have to do to get this to work?

I'd appreciate any help.

2
  • don't echo it just write the script outside of your php tags, also that example doesn't have any php in it...so give it a .html extension instead of .php Commented Apr 5, 2013 at 0:01
  • 6
    Step 1: Post code with your question. Otherwise we're shooting blindfolded after being spun around in an office chair for three hours. Commented Apr 5, 2013 at 0:02

2 Answers 2

1

With php you can just add the html before the HTML link before

<?php

or after

?>

and it will be interpreted it as normal HTML. Just use

<script src="file goes here">

to link the jquery script, and put you code in:

<script></script>
Sign up to request clarification or add additional context in comments.

Comments

0

As @nathan hayfield suggests, you can put it outside of the PHP tags. If you want to use echo or print inside of the PHP tags, then you can do it like this:

echo '<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
      <script type="text/javascript" src="js/jquery.twitter.feed.js"></script>';

Or you can use this syntax, which I often do when I have a string with both quotes and single quotes:

$extra_js = <<<EXTRAJS
<script type="text/JavaScript">
$(document).ready(function() {  // using first example from the site you mentioned
    $('#twitter-feed').dcTwitterFeed({
        id: '#designchemical',
        tweetId: 'designchemical'
    });
});
</script>
EXTRAJS;

echo $extra_js;  // or pass it to a function or whatever

4 Comments

Ok thanks guys that seems to work for that but I'm getting a syntax error when I put in <script type="text/JavaScript"> $(document).ready(function() { $('#twitter-feed').dcTwitterFeed({ id: '#designchemical', tweetId: 'designchemical' }); }); </script>using the same method. Can I put this in the PHP file?
We would need to see your code including the php, etc... You can edit your question and add the code. Also, if you find an answer that answers your question please accept it.
Ok thanks I'll do that. Regarding the above the "No that example doesn't have php in it" referred to the tutorial not having php in as opposed to me saying no to his answer. Sorry if it sounded like that I was not at all saying no to his answer! I greatly appreciate the time people put into any response at all.
Ok I got it working thanks to your suggestions. thanks for your help all!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.