FAQ

Just Custom Fields

How to print the JCF Field manually in the template?

When you create a new field, you are specifying the following general settings for all field types: Title, Description, Slug, and Enabled. The Title and Description fields you will see on the edit post page. The Enabled field is responsible for the display of the field on edit post page. Slug is used as a key to store field value and to display field value on the front-end.

newfield How to print the JCF Field manually in the template?

To manually use the fields’ contents in the template you may use the standard WordPress function get_post_meta ($post_id, $key, $singe), where

  • $post_id – ID is the post indicating which field you want to use
  • $key is the value of the Slug settings of the desired field
  • $single indicates in what format the value will be returned, we recommend setting this key to true

Let’s see how to use manual print of fields by creating an Ad page. To do this we will create a new Post Type and bind to it a few fieldsets in which we use almost all types of possible fields, that are provided by our plugin:

Input Text & Textarea

As described above, to obtain the desired value, we use a standard WordPress function get_post_meta.
In this example, we will print the name of the realtor. When displaying the contents of the usual text box we can protect ourselves by using standard WordPress functions like esc_html, which can protect the site from xss attacks.

<?php echo esc_html(get_post_meta(get_the_ID(),'_realtors_name', true)); ?>

When outputting the field type “Textarea” we cannot use this feature. This field can contain tags from the visual editor, which will be ruined, but we can use the standard filter the_content for these fields.

<?php echo apply_filters('the_content', get_post_meta(get_the_ID(),'_advert_address', true)); ?>

Select & Select Multiple

The Select field returns the selected value as a string. Select Multiple returns an array of the selected values.

<?php if( $type = get_post_meta(get_the_ID(), '_adver_type', true) ) : ?>
    <p class="advert_type"><?php _e('Type'); ?> : <?php echo $type; ?></p>
<?php endif; ?>
<?php if( $types = get_post_meta(get_the_ID(), '_adver_multiple_type', true) ) : ?>
    <p class="advert_type"><?php _e('Type'); ?> : <?php echo implode(', ',$types); ?></p>
<?php endif; ?>

Simple Media

Now we will review how we can use the Simple Media field. As you may already know, this field stores the ID of the selected file. In this example, we will have the realtor’s photo.

<?php 
    $image_id = get_post_meta(get_the_ID(), '_realtor_photo', true); 
        if($image_id) : 
            $image = wp_get_attachment_image($image_id, 'small'); 
            $full_image_src = wp_get_attachment_image_src($image_id, 'full');
?>
            <a href="<?php echo $full_image_src[0]; ?>" class="lightbox"><?php echo $image; ?></a>
		
<?php   endif; ?>

Table

Data from the field type “Table” represents a matrix array, where the rows are table lines added to the post edit page and each row is an associated array of table row cells, keys are specified in the field settings.

<div class="contacts">
<?php
    if( $contact_info = get_post_meta(get_the_ID(), '_realtor_contacts', true) ) :
        foreach( $contact_info as $contact ) :
?>
            <p>
                <strong><?php echo esc_html($contact['title']); ?> : </strong>
                <span><?php echo esc_html($contact['content']); ?></span>					
                <span><?php echo esc_html($contact['time']); ?></span>
            </p>
<?php
	endforeach;
    endif;
?>
</div>

Date Picker

The field type “Date Picker” stores the selected date in a format specified in the field settings. After the contents of the field is obtained it can be transformed into any other date format by using the PHP function date() and strtotime().

<?php 
    if( $date = esc_html( get_post_meta(get_the_ID(), '_release_date', true) ) ) : 
        $date = date('l, j F, Y', strtotime($date));
?>
        <div class="release_date"><?php echo $date; ?></div>
<?php endif; ?>

Checkbox

The field type “Checkbox” can return two different type values. If only one option is specified in the field settings, then you will receive either an empty string or a string with the checkbox id option (set in field settings). Such value can be used as a boolean flag.

<?php if( get_post_meta(get_the_ID(), '_hot_advert', true) ) : ?><span class="hot"></span><?php endif; ?>

In the case where you have multiple choice values specified in the field settings, the value is retrieved from the database field and you get an array where the number and value of items corresponds to the marked checkboxes on the post edit page

<?php 
if( $featured = get_post_meta(get_the_ID(), '_hot_advert', true) ) : 
    foreach( $featured as $class) : 
?>
        <span class="<?php echo $class; ?>"></span>
<?php 
    endforeach;
endif; 
?>

Collection

The field type “Collection”, just like “Table”, saves its values  in the matrix array. It is basically an unlimited number of rows with a predetermined number of fields in the options. Internal collection fields has the same values of plugin fields described above, and we should work with them the same way.

<?php 
$slider = get_post_meta(get_the_ID(), '_advert_additional_image', true);
if(!empty($slider)) :
    foreach($slider as $slide) : 
        if(!$slide['_enabled']) continue; 
        ?>
            <div class="slide">
                <?php if(!empty($slide['_image'])) : ?>
                    <div class="slider_image"><?php echo wp_get_attachment_image($slide['_image'], 'full'); ?></div>
                <?php endif; ?>

                <div class="slide_content">
                <?php
                    if(!empty($slide['_title'])) echo '<h2>'.esc_html($slide['_title']).'</h2>';
                    if(!empty($slide['_description'])) echo $slide['_description'];
                ?>
                </div>
            </div>
<?php 
        endforeach; 
    endif; 
?>

Related Content

The “Related Content” field type is an array with the chosen post ids of specific post type, which was specified in field settings. Using this array, you can generate the new WP_Query and display the required fields of the selected records.

<?php 
$related = get_post_meta(get_the_ID(), '_related_advert', true);
if(!empty($related)) :
    $related_adverts = get_posts(array('include' => $related, 'numberposts' => -1, 'post_type' => 'agenda'));
    if(!empty($related_adverts)) :
?>
        <div class="related_content">
            <h2><?php _e('Related Adverts'); ?></h2>

            <?php foreach($related_adverts as $advert) : ?>
                <div class="item-box">
                    <div class="image-holder"><?php echo get_the_post_thumbnail($advert->ID, 'small'); ?></div>
                    <h3><?php echo esc_html($advert->post_title); ?></h3>
                    <a href="<?php the_permalink($advert); ?>"><?php _e('Read more'); ?></a>
                </div>
            <?php endforeach; ?>
        </div>
<?php
    endif;
endif;
?>