Dynamic meta descriptions and keywords in Drupal Views
We created a view with exposed filters and arguments in Drupal so users could search for a particular product by city and state. For the purposes of this, we’ll call them widgets. We wanted to have custom meta descriptions and keywords for each of these city and state pages.
This requires the Nodewords module and you’ll need to create a custom module to take advantage of the functionality of the Nodewords API.
The arguments we used in the url for the view are as follows:
- arg 0 is “widgets” or the custom content type.
- arg 1 is the city.
- arg 2 is the state.
So a path would look like this: widgets/springfield/mo
We also included the ability to search all cities in a state by using: widgets/search/mo
Or all of the United States with: widgets/search/us
(This is done simply by replacing the “ALL” wildcard term that Views uses)
Using these arguments from the url, we were able to create meta descriptions and keywords on the fly:
function digitalstax_customizations_nodewords_tags_alter(&$tags, $parameters) {
if(arg(0) == 'widgets') {
if(arg(1) != 'search' && arg(2) != 'us'){
$city = ucwords(str_replace( "-", " ", arg(1)));
$state = strtoupper(str_replace( "-", " ",arg(2)));
$tags['description'] = "Find widgets for sale in " . $city.", ". $state . " at The Widget Place.";
$tags['keywords'] = "". $city .", " . $state . ", widgets, what-nots, the widget place";
}
if(arg(1) == 'search'){
$state = strtoupper(str_replace( "-", " ",arg(2)));
$tags['description'] = "Find widgets for sale in ". $state . " at The Widget Place.";
$tags['keywords'] = "". $state . ", widgets, what-nots, the widget place";
}
if(arg(2) == 'us'){
$tags['description'] = "Find widgets for sale in the United States at The Widget Place.";
$tags['keywords'] = "united states, us, widgets, what-nots, the widget place";
}
}
}
| Print article | This entry was posted by Raymond on April 18, 2010 at 5:20 am, and is filed under Drupal, Nodewords, Views. Follow any responses to this post through RSS 2.0. Both comments and pings are currently closed. |
Comments are closed.