Adding Dynamic Meta title and description by URL Match with REQUEST_URI condition

For adding meta titles and descriptions we have multiple options to do this by adding plugins or we can add custom meta tags by adding code with conditions . Here is a below example how to add dynamic meta titles and descriptions page wise.

for example refer the below code for about and contact page adding under the head section of your activated theme.

Adding title and description by URL Match with REQUEST_URI condition

code

  1. <?php
  2.  
  3. if($_SERVER['REQUEST_URI']== '/about/') { ?>
  4.  
  5. <title>Adding this title for about us page</title>
  6. <meta name="description" content="Adding this meta description for about us page" />
  7.  
  8. <?php }
  9.    
  10. else if($_SERVER['REQUEST_URI']== '/contact-us/')
  11. { ?>
  12.  
  13. <title>This is the title tag for contact page</title>
  14. <meta name="description" content="This is the meta description for contact page" />
  15.  
  16. <?php }
  17.    
  18. else { ?>
  19.    
  20. <title><?php wp_title(); ?></title>
  21.  
  22. <?php } ?>

See below conditions for adding meta title and descriptions by the page wise conditions

php

  1. <?php if(is_home()) { ?>
  2. <title>Adding title for home page</title>
  3. <meta name="description" content="Adding this meta description for home page" />
  4. <?php } ?>
  5.  
  6. <?php if(is_page()) { ?>
  7. <title>Adding this title for all pages</title>
  8. <meta name="description" content="Adding this meta description for all pages" />
  9. <?php } ?>
  10.  
  11. <?php if(is_page('20' )) { ?>
  12. <title>Adding this title for particular page by id</title>
  13. <meta name="description" content="Adding this meta description for page id" />
  14. <?php } ?>
  15.  
  16. <?php if(is_single()) { ?>
  17. <title>Adding this title for blog post page</title>
  18. <meta name="description" content="Adding this meta description for blog post page" />
  19. <?php } ?>
  20.  
  21. <?php if(is_404()) { ?>
  22. <title>Adding this title for dead page</title>
  23. <meta name="description" content="Adding this meta description for dead 404 page" />
  24. <?php } ?>
  25.  
  26. <?php if(is_search()) { ?>
  27. <title>Adding this title for search page</title>
  28. <meta name="description" content="Adding this meta description for search page" />
  29. <?php } ?>
  30.  
  31. <?php if(is_archive()) { ?>
  32. <title>Adding title for archive page</title>
  33. <meta name="description" content="Adding description for archive page
  34. <?php } ?>

Leave a Reply

Your email address will not be published. Required fields are marked *