Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialRob McClara
19,257 PointsHow do I use a variable in the keywords meta tag?
In the SEO course, it was suggested to use unique keywords on each page. I am 'including' the header on each page. How do I assign a variable to the content attribute? i.e.:
<meta name="keywords" content= " ' " . <?php echo $keywords ?> . " ' >" ;
How would I check to make sure the code works?
Colin Marshall
32,861 PointsHey Rob, I fixed your code for you. Please watch the video on the right "Tips for asking questions" to see how to post code on the forum
Colin Marshall
32,861 PointsWhat reason(s) are you using the keywords meta tag? Are you aware that Google does not even look at this tag?
3 Answers
Andrew Shook
31,709 PointsRob, what does the $keywords variable hold? Is it just a string or is it an array of strings? Also, since this is just html you don't need to concatenate the php code block. Remove the periods and single quotes and just do this:
<meta name="keywords" content= "<?php echo $keywords; ?> " >
If $keywords in an array, then I would use the implode function to convert the array into a string.
<meta name="keywords" content= " <?php echo implode(", " , $keywords); ?> ">
Since you will be using this on most of your pages it might be good to stick this into a functions file that you can include on all of your pages.
<?php
function the_keywords(){
if(Isset($keywords)){
echo implode(", " , $keywords);
}else{
echo "";
}
}
Rob McClara
19,257 PointsThank you Colin for the tips on the right. I appreciate that. Thank you for pointing out that keywords are not used by Google. I meant to say meta description! My apologies. How do I use a variable for the description? Then test to make sure the code is correct?
Andrew Shook
31,709 Pointswhat are you using to build this website, a CMS or framework?
Rob McClara
19,257 PointsI am using HTML, PHP, and Bootstrap. No CMS.
Rob McClara
19,257 PointsEach of my webpages are .php files, not html. Since that's the case, wouldn't I need to add the concatenated quotations to your first example?
Andrew Shook
31,709 Pointsthe html isn't between and opening and closing php tag then no.
Rob McClara
19,257 PointsThank you Andrew. The variable is a simple string that contains the meta description for that individual page. I will assign the value to the variable before including the header.php file. Then, inside the header.php file, the meta description will reference the variable and provide a unique meta description for that page. Your answer was helpful. Thank you!
Rob McClara
19,257 PointsRob McClara
19,257 PointsFor some reason my code snippet didn't display. Let me try again. meta name="keywords" content = " ' ". $keywordsVariable . " '>"
I realize the code is missing syntax characters, but the form won't allow it to be displayed properly.