Is this meta tags in header.php correct as google plus is u

so what should be my correct code plz guide

Try this - Place this code above the doctype in your header.php file:

<?php 
	$postid = get_the_ID(); 
	$excerpt = get_the_excerpt( $postid );
?>

Then your description code would be:

<meta name="description" content="<?php echo (NULL != $excerpt) ? $excerpt : '' ; ?>" />

in your <head>

1 Like

I’ve placed code still <meta name=“description” is empty

Hello,
One approach to dynamically add page description / meta is to use a plugin named “Advanced Custom Fields” where you create a custom field with this slug for exemple “page_description” and then on each page/post you manually populate this field (there is a meta box on edit page also). At the end you could use ACF dedicated function “get field” (see the documentation) to display in the necessary informations.
As your question indicate that you prefer not to use plugins due to your hosting type, the same result can be achieved with WP custom fields, in this case you need to get the page/post ID first then display the slug of the custom field (if have posts, get the id > display the slug for this id).
Using this give you total liberty on post/page description (since it is limited in length) to provide a complete sentence, with your keywords and good description,not a 200 chars excerpt stripped that could say nothing. Prefer to use one meta description in the code.
Cheers !

The OP has already stated that they do not want to use a plugin for this.
Please read the topic before posting.

Excerpts only work for posts, not pages. And if you have not written an excerpt for the post on its edit page, there will not be an excerpt to display unless the theme you have auto-generates the excerpts.

no i dont want manual options ,do u have auto-excerpt option only as i’ve 2000 post on my site

Try putting a loop in your <head>. This seemed to work for my local test site. But automatically generated excerpts have […] at the end, which you might not want in a description. I will see if I can remove that.

<?php if (is_single() || is_page() ) : if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <?php remove_filter('the_excerpt', 'wpautop'); ?>
	<meta name="description" content="<?php the_excerpt(); ?>" />
<?php endwhile; endif; endif; ?>

Put this in your functions.php to remove the [...] at the end of the excerpt.

function trim_excerpt($text) {
  return substr($text, 0, -11);
}
add_filter('get_the_excerpt', 'trim_excerpt');

Tnx for help ,i’ve added code plz check

Now bcoz of u it shows desired description

Below is title which shows title ,but is it right code ?
<?php wp_title(''); ?><?php if (!(is_404()) && (is_single()) || (is_page()) || (is_archive())) { ?><?php } ?><?php bloginfo('name'); ?>

Only BIG Problem is google still doesnt fetch RIGHT title,description ,IVE TRIED SEARCHING EVERYWHERE

My current OG meta is

**<meta property="og:title" content="<?php the_title();?>" />**
**<meta property="og:description" content="<?php the_excerpt();?>" />**
**<meta property="og:type" content="article"/>**
**<meta property="og:url" content="<?php the_permalink();?>" />**

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.