Reputation: 2542
In terms of Cake PHP, I am a newbie.
Some of the pages in my site giving meta keywords like below.
<meta content="Array" name="keywords"></meta>
I found that meta data comes from app/views/layouts/defautlt.ctp
, where we have the below code to display meta keywords.
<meta name="keywords" content="<?php echo $meta_keywords; ?>" />
I am stranded here. How can I find that what is the error.
Note: Some Pages displaying meta keywords correctly. But most of the pages showing Array
as keywords.
I have also added the meta description code below.
<meta name="description" content="<?php echo reset($meta_description); ?>" />
What could be the reason to display keywords as Array
?
Upvotes: 0
Views: 319
Reputation: 4397
The code in my comment works and that's fine, but you should understand why it works.
When you echo an array in PHP, it does not print the contents of the array. It only prints "Array".
The reason you sometimes see the meta keywords correctly is because $meta_keywords contains a string. Sometimes you see only "Array" because in these cases $meta_keywords contains an array of strings.
$meta_keywords = "my_keyword";
echo $meta_keywords; // prints "my_keyword"
$meta_keywords = array("kw1", "kw2", "kw3");
echo $meta_keywords; // prints "Array"
When you want to print the contents of an array, you can use the print_r() function.
$meta_keywords = array("kw1", "kw2", "kw3");
print_r($meta_keywords);
// prints:
// Array ( [0] => kw1
// [1] => kw2
// [2] => kw3
//)
When you want to join the contents of an array into a string, you can use the implode() function. To check if a variable is an array, use the is_array() function.
The ?: is the ternary operator in PHP. The code:
echo (is_array($meta_keywords)) ? implode(",", $meta_keywords) : $meta_keywords;
is equivalent to:
if (is_array($meta_keywords)) {
echo implode(",", $meta_keywords);
} else {
echo $meta_keywords;
}
So when
$meta_keywords = "my_keyword",
is_array()
returns false and you get output "my_keyword".
When
$meta_keywords = array("kw1", "kw2", "kw3"),
is_array()
returns true and you get output "kw1,kw2,kw3".
Hope this is helpful.
Upvotes: 1