WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display Custom Meta Box labels (3 posts)

  1. damienoneill2001
    Member
    Posted 7 months ago #

    Hi guys,

    I hope this is the right section!

    I have created a Custom Post Type and Custom Meta Box with a number of fields inside. I am currently able to display the information from the Custom Meta Box fields on the front end of the site, but am struggling with two things.

    The first thing I want to do is to display the titles/labels of the fields within the Meta Box and secondly, I only want to display this information if there is actually something entered into the fields.

    I hope someone can help!

    The code I am using in my template is as follows:

    <div id="field_1" class="meta_field_box">
        <?php $field_1 = get_post_meta($post->ID, "custom_field_1", true);
    	echo $field_1; ?>
     </div>
     <div class="clear"></div>
     <div id="field_2" class="meta_field_box">
        <?php $field_2 = get_post_meta($post->ID, "custom_field_2", true);
    	echo $field_2;?>
     </div>
     <div class="clear"></div>
    .....etc

    Any articles I find on this seems to just focus on either amending the title names in the backend or displaying just the field information.

    Thanks guys

    Damien

  2. damienoneill2001
    Member
    Posted 7 months ago #

    Hi all,

    Ok, I have managed to add the titles of the fields using simple HTML, however, I still need help with displaying these fields (and their titles) only when there is actually content in them :)

    For anyone who needs it, here is the code so far.....

    <div id="field_1" class="meta_field_box">
        <?php $field_1 = get_post_meta($post->ID, "custom_field_1", true);?> //I closed the php element here to add the html line below
        <span class="meta_field_title>Field Title</span>
    	<?php echo $field_1; ?>
     </div>
     <div class="clear"></div>
     <div id="field_2" class="meta_field_box">
        <?php $field_2 = get_post_meta($post->ID, "custom_field_2", true);?> //same here
        <span class="meta_field_title>Field Title</span>
    	<?php echo $field_2;?>
     </div>
     <div class="clear"></div>
  3. damienoneill2001
    Member
    Posted 7 months ago #

    Hey again,

    Ok, I have it working. I'm not sure how correct this code is but it is doing the trick for me and may come in useful to others in the future....

    <?php $field_1 = get_post_meta($post->ID, "custom_field_1", true);
         if (!empty ($field_1))
             echo '<div id="field_1" class="meta_field_box"><span class="meta_field_title">Field Title</span><br />'. $field1.'</div>';
    ?>

    Basically, all this is doing is defining $field_1 as the data from Meta Box Field 1.
    Then, if $field_1 is empty, display a div with ID Field_1 for styling purposes. Inside the div is a span class for the Title. Then, do a line break down to the actual data from field_1.

    If anyone can improve on that, feel free :)

    Damien

Reply

You must log in to post.

About this Topic