How to use meta tag and description dynamically for wordpress

To use meta tag and description in wordpress dynamically, we use the wordpress tags as a meta tags and excert content use as a meta description.

As per the requirement description should be 150 words. so we find this content using the “get_post() function

For Example:

<?php $post_id = the_ID(); $post = get_post($post_id); echo strip_tags(substr($post->post_content,0 , 150)); ?>

And to find the meta tags without hyperlink, we use “get_the_tags()” function.

For Example:

<?php $posttags = get_the_tags(); if ($posttags) { foreach($posttags as $tag) { $taglist[] = $tag->name;  } echo implode(“, “, $taglist); } ?>

Use these script to make dynamic meta tag and description in your wordpress blog.

The complete head code is:

   <title><?php bloginfo(‘name’); ?><?php wp_title(); ?></title>

<meta http-equiv=”Content-Type” content=”<?php bloginfo(‘html_type’); ?>; charset=<?php bloginfo(‘charset’); ?>” />

<meta name=”description” content=”<?php $post_id = the_ID(); $post = get_post($post_id); echo strip_tags(substr($post->post_content,0 , 150)); ?>” />

<meta name=”keywords” content=”<?php $posttags = get_the_tags(); if ($posttags) { foreach($posttags as $tag) { $taglist[] = $tag->name; } echo implode(“, “, $taglist); } ?>” />

<meta name=”robots” content=”INDEX,FOLLOW” />

Use this code in your head section.

 


To seek challenging roles and to achieve excellence in the field of Open Source Web Technology with a profound spirit of commitment and dedication.

Share This Post

Related Articles

© 2017 Sumit Bansal. All rights reserved. Site Admin · Entries RSS · Comments RSS
Powered by WordPress · Designed by Theme Junkie