WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Custom Field Display Issue (4 posts)

  1. themilkmen
    Member
    Posted 2 years ago #

    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

  2. egado
    Member
    Posted 2 years ago #

    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 :)

  3. themilkmen
    Member
    Posted 2 years ago #

    Thank you that works perfectly

  4. egado
    Member
    Posted 2 years ago #

    Your welcome!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.