0

I have successfully added custom meta tags to metaboxes on my pages. The issue I am having is when the metabox has no data it is still showing in the html. This is the code I have got, does anyone have any ideas on how to not show this?

    <!-- Google Meta Tags -->
<meta name="citation_title" content="<?php if(get_post_meta($post->ID, "vw_citation_title", true) !='' ) echo get_post_meta($post->ID, "vw_citation_title", true);?>">
<meta name="citation_author" content="<?php if(get_post_meta($post->ID, "vw_citation_author", true) !='' ) echo get_post_meta($post->ID, "vw_citation_author", true);?>">
<meta name="citation_author" content="<?php if(get_post_meta($post->ID, "vw_citation_author_1", true) !='' ) echo get_post_meta($post->ID, "vw_citation_author_1", true);?>">
<meta name="citation_author" content="<?php if(get_post_meta($post->ID, "vw_citation_author_2", true) !='' ) echo get_post_meta($post->ID, "vw_citation_author_2", true);?>">
<meta name="citation_author" content="<?php if(get_post_meta($post->ID, "vw_citation_author_3", true) !='' ) echo get_post_meta($post->ID, "vw_citation_author_3", true);?>">
<meta name="citation_author" content="<?php if(get_post_meta($post->ID, "vw_citation_author_4", true) !='' ) echo get_post_meta($post->ID, "vw_citation_author_4", true);?>">
<meta name="citation_author" content="<?php if(get_post_meta($post->ID, "vw_citation_author_5", true) !='' ) echo get_post_meta($post->ID, "vw_citation_author_5", true);?>">
<meta name="citation_author" content="<?php if(get_post_meta($post->ID, "vw_citation_author_6", true) !='' ) echo get_post_meta($post->ID, "vw_citation_author_6", true);?>">
<meta name="citation_author" content="<?php if(get_post_meta($post->ID, "vw_citation_author_7", true) !='' ) echo get_post_meta($post->ID, "vw_citation_author_7", true);?>">
<meta name="citation_author" content="<?php if(get_post_meta($post->ID, "vw_citation_author_8", true) !='' ) echo get_post_meta($post->ID, "vw_citation_author_8", true);?>">
<meta name="citation_author" content="<?php if(get_post_meta($post->ID, "vw_citation_author_9", true) !='' ) echo get_post_meta($post->ID, "vw_citation_author_9", true);?>">
<meta name="citation_publication_date" content="<?php if(get_post_meta($post->ID, "vw_citation_publication_date", true) !='' ) echo get_post_meta($post->ID, "vw_citation_publication_date", true);?>">
<meta name="citation_journal_title" content="<?php if(get_post_meta($post->ID, "vw_citation_journal_title", true) !='' ) echo get_post_meta($post->ID, "vw_citation_journal_title", true);?>">
<meta name="citation_volume" content="<?php if(get_post_meta($post->ID, "vw_citation_volume", true) !='' ) echo get_post_meta($post->ID, "vw_citation_volume", true);?>">
<meta name="citation_issue" content="<?php if(get_post_meta($post->ID, "vw_citation_issue", true) !='' ) echo get_post_meta($post->ID, "vw_citation_issue", true);?>">
<meta name="citation_firstpage" content="<?php if(get_post_meta($post->ID, "vw_citation_first_page", true) !='' ) echo get_post_meta($post->ID, "vw_citation_first_page", true);?>">
<meta name="citation_lastpage" content="<?php if(get_post_meta($post->ID, "vw_citation_last_page", true) !='' ) echo get_post_meta($post->ID, "vw_citation_last_page", true);?>">
<meta name="citation_pdf_url" content="<?php if(get_post_meta($post->ID, "vw_citation_pdf_url", true) !='' ) echo get_post_meta($post->ID, "vw_citation_pdf_url", true);?>">
2
  • The metabox itself will show up if it is registered regardless of content. That is how meta boxes work, and I am puzzled by the need to display a meta box only if it has content. How does the meta box get content in the first place if it only shows up after it has content?
    – s_ha_dum
    Commented May 5, 2014 at 15:20
  • I think I have the code wrong. What I specifically need to is to output <meta name = "citation_title" content = "custom text"> into my header when there is data inputted into the metabox. I am going to try another way, but if someone has any ideas how to go about solving this it would be a great help! Thanks.
    – Chris
    Commented May 5, 2014 at 19:17

1 Answer 1

1

This is really just a PHP question, but since there is no Core function to do this...

I'd use !empty(). Otherwise just echo a string.

$mcontent = get_post_meta($post->ID, "vw_citation_title", true);
if(!empty($mcontent)) {
    echo '<meta name = "citation_title" content = "''.$mcontent.'" >';
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.