Search code examples
phpwordpressfunctionmeta-tags

how to close if fuction of that code without error?


this code i created it to add meta keywords tag for only posts header with execluding for all website pages ( home & about & contact & blog & ...etc ) i add this code to function.php and each time i got error that i have to close if function with endif and each time i tried that i got error.. so please help

add_action('wp_head', function (){
  ?>
  <?php 
    if ( is_single() ) {
    echo get_post_meta( get_the_ID(), 'meta-head', true );  
  $postTags = get_the_tags();
  $tagNames = array();
  foreach($postTags as $tag) {
    $tagNames[] = $tag->name;          
      }
?>
<meta name="keywords" content="<?php echo implode($tagNames,","); ?>" />
  <?php
    }

Solution

  • You forgot to close the function

    add_action('wp_head', function (){
      if ( is_single() ) { 
        echo get_post_meta( get_the_ID(), 'meta-head', true ); 
        $postTags = get_the_tags(); 
        $tagNames = array(); 
        foreach($postTags as $tag) { 
          $tagNames[] = $tag->name; 
         } 
         ?> 
         <meta name="keywords" content="<?php echo implode($tagNames,","); ?>" /> 
      <?php 
      }
     } // This one here