admin管理员组

文章数量:1122846

Wordpress renders a meta description tag with the content of the blog post as the value for the description.

I already generated in the header.php what I want the description to be, no matter what. However, the old one is still in there. I checked single-post.php and header.php and there is nothing rendering the meta description. So it HAS to be the wp_head() function.

Is there anyway I can put something in my functions.php or something to ensure that on certain pages the meta description is removed from wp_head()?

Wordpress renders a meta description tag with the content of the blog post as the value for the description.

I already generated in the header.php what I want the description to be, no matter what. However, the old one is still in there. I checked single-post.php and header.php and there is nothing rendering the meta description. So it HAS to be the wp_head() function.

Is there anyway I can put something in my functions.php or something to ensure that on certain pages the meta description is removed from wp_head()?

Share Improve this question edited Jun 18, 2018 at 15:35 Jan Doggen 16911 bronze badges asked Mar 8, 2017 at 22:30 Josh HollyJosh Holly 111 gold badge1 silver badge3 bronze badges 2
  • Have you tried switching themes to make certain it isn't your theme, and have you tried disabling all your plugins to see if any of them are setting it? single-post.php and header.php are not the only places in a theme one could set a meta description, and the theme is not the only option either. – WebElaine Commented Mar 8, 2017 at 22:38
  • It's not the theme, seeing that I have the same theme on my test site and it doesn't do that. I literally have no clue where the meta tag is coming from. I don't want to disable all the plugins on my main site as it is a large website that I hate to see go down. – Josh Holly Commented Mar 8, 2017 at 23:21
Add a comment  | 

3 Answers 3

Reset to default 1

You should be able to remove the existing description tag with the following code:

remove_action( 'wp_head', '_wp_render_title_tag', 1 );

Put the above snippet inside the same function you are using to conditionally add a new tag so there is always only a single description tag at a time.

If you want to remote <meta name="description" content="" /> in html head, Add

remove_action('wp_head', 'description');

into your function.php.

For change the content of the meta description:

<?php 
$description = '';
if( is_single() or is_page() ){
    global $post;
    $description = get_post_meta( $post->ID, $this->meta_key, true );
} ?>
<?php if( $description ): ?>
    <meta name="description" content="<?php print $description; ?>"
<?php endif ?>

本文标签: phpRemove meta description on certain pages