Support » Fixing WordPress » Custom Field Display Issue

  • Resolved themilkmen

    (@themilkmen)


    I am using the following code to display Meta Keywords and Meta Decriptions using custom fields.

    <?php
    	if($meta_keywords= get_post_meta(get_the_ID(), 'meta_keywords', true))
    	echo '<meta name="keywords" content="' ; echo $meta_keywords ; echo '" />' ;
    	?>
    <?php
    	if($meta_description= get_post_meta(get_the_ID(), 'meta_description', true))
    	echo '<meta name="description" content="' ; echo $meta_description ; echo '" />' ;
    	?>

    The code works perfectly as long as I populate the custom fields however if do not add the custom field to page each page ends up with the ” /> ” /> showing at the top of the page.

    Would anyone be able to shed some light on what I am doing wrong.

    Many thanks

    James

Viewing 3 replies - 1 through 3 (of 3 total)
  • Try this:

    <?php if ( get_post_meta($post->ID, 'meta_keywords', true) ) : ?>
      <meta name="keywords" content="<?php echo get_post_meta($post->ID, 'meta_keywords', true) ?>" />
    <?php endif; ?>
    <?php if ( get_post_meta($post->ID, 'meta_description', true) ) : ?>
      <meta name="description" content="<?php echo get_post_meta($post->ID, 'meta_description', true) ?>" />
    <?php endif; ?>

    Let me know if it works for you 🙂

    Thread Starter themilkmen

    (@themilkmen)

    Thank you that works perfectly

    Your welcome!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Custom Field Display Issue’ is closed to new replies.