You are not logged in.
- Topics: Active | Unanswered
#1 2009-12-08 20:27:00
- quaker
- Member
- From: USA
- Registered: 2008-05-09
- Posts: 90
- Website
need alittle help with the coding... for keyword tagging
This pulls all tag words from the db and displays them in the header of wordpress. I would like to limit and randomize this to a max of 500 words. Since the seo site that I use tells me this.
Each time the page is pulled it changes and displays the tag as keyword in the header for that post.
This tag contains too many characters.
This tag contains 710 characters. This is too many for what we would consider a 'robot friendly' web page. The maximum number of characters we recommend for this tag is 500
<?php global $post;
if( is_single() || is_page() ) :
$tags = get_the_tags($post->ID);
if($tags) :
foreach($tags as $tag) :
$sep = (empty($keywords)) ? '' : ', ';
$keywords .= $sep . $tag->name;
endforeach;
?>
<meta name="keywords" content="<?php echo $keywords; ?>" />
<?php
endif;
endif;
?>
So, Can someone help me with this?
Q
My stuff or my style might sux, but atleast I'm willing to help when I can.
Don't be stupid and help ! We are the stupid one's !!!
Offline
#2 2009-12-08 21:22:02
- Franz
- Lead developer
- From: Germany
- Registered: 2008-05-13
- Posts: 6,742
- Website
Re: need alittle help with the coding... for keyword tagging
Not 500 tags, 500 characters
This is rather hack-ish, but it should work. It truncates the tag string at the end and then removes a possibly truncated tag from the end:
<?php global $post;
if( is_single() || is_page() ) :
$tags = get_the_tags($post->ID);
if($tags) :
$keywords = '';
foreach($tags as $tag) :
$sep = (empty($keywords)) ? '' : ',';
$keywords .= $sep . $tag->name;
endforeach;
if (strlen($keywords) > 500) :
$keywords = substr($keywords, 0, 500);
while (substr($keywords, -1) != ',')
$keywords = substr($keywords, 0, -1);
// Remove the comma
$keywords = substr($keywords, 0, -1);
endif;
?>
<meta name="keywords" content="<?php echo $keywords; ?>" />
<?php
endif;
endif;
?>
Note: I also removed the space from the comma separating the tags. That should give you a little more room and doesn't matter as far as I know.
Offline
#3 2009-12-08 21:25:10
- Reines
- Administrator
- From: Scotland
- Registered: 2008-05-11
- Posts: 3,197
- Website
Re: need alittle help with the coding... for keyword tagging
Slightly neater version:
<?php
global $post;
define('SEPARATOR', ',');
define('MAX_LENGTH', 500);
if(is_single() || is_page())
{
$tags = get_the_tags($post->ID);
if($tags)
{
shuffle($tags); // Randomize the tag order
reset($tags); // Reset the array to the beginning (just in-case)
$keywords = '';
// while there's something in the array and the length isn't over MAX_LENGTH characters (note: don't count the SEPARATOR since it is removed from the start later)
while ((list(, $cur_tag) = @each($tags)) !== false && strlen($keywords) + strlen($cur_tag) <= MAX_LENGTH)
$keywords .= SEPARATOR.htmlspecialchars($cur_tag->name);
// Remove the first SEPARATOR
$keywords = substr($keywords, strlen(SEPARATOR));
?>
<meta name="keywords" content="<?php echo $keywords; ?>" />
<?php
}
}
Offline
#4 2009-12-08 21:32:05
Offline