Number Field

Overview

This Default Field Type is available in WordPress Creation Kit free version. The Number Field is used to add a number.

Creating a Number Field

To add a Number Field to a previously created Custom Meta Box, under the Meta Box Fields tab simply insert a Field Title and under Field Type make sure you select Number.

WordPress Creation Kit - Custom Fields Creator - Meta Box Fields - HTML Field

The Number Field contains options to customize it:

  1. Description – Allows you to specify a description for the Number Field
  2. Required – Select whether or not the field is required
  3. Default Value – Set a default value for the field
  4. Min Number Value – Min allowed number value (0 to allow only positive numbers). Leave it empty for no min value
  5. Max Number Value – Max allowed number value (0 to allow only negative numbers). Leave it empty for no max value
  6. Number Step Value – Step value 1 to allow only integers, 0.1 to allow integers and numbers with 1 decimal. To allow multiple decimals use for eg. 0.01 (for 2 decimals) and so on. You can also use step value to specify the legal number intervals (eg. step value 2 will allow only -4, -2, 0, 2 and so on). Leave it empty for no restriction

Page, Post or Custom Post Type Edit Screen

This is how the Number Field we created above looks like in the Edit Screen:

WordPress Creation Kit - Custom Fields Creator - Meta Box Fields - Number Field (Editor)

Template usage

The following examples are for a Custom Meta Box with the “Group Name” argument “my_meta_name“. Make sure to replace this with the “Group Name” you have set up. The Custom Meta Box below is setup to be attached to a post.

WordPress Creation Kit - Custom Fields Creator - Meta Box Arguments - Template

Using the WCK Custom Fields API

The Number Field returns a string containing a number.

For a Single Meta Box

To output a value inside The Loop we use the function the_cfc_field():

1
Value: <?php the_cfc_field('my_meta_name', 'my-number'); ?>

To assign the value to a variable we use the function get_cfc_field():

1
<?php $first_number = get_cfc_field('my_meta_name', 'my-number'); ?>

For a Repeater Meta Box

To output all the “My Number” entries from the repeater field we use the functions get_cfc_meta() and the_cfc_field():

1
2
3
4
5
<?php 				
foreach( get_cfc_meta( 'my_meta_name' ) as $key => $value ){
    the_cfc_field( 'my_meta_name','my-number', false, $key );
}
?>

To output a specific “My Number” entry from the repeater field (e.g. the second entry), we use the function the_cfc_field():

1
<?php the_cfc_field( 'my_meta_name','my-number', false, 1 ); ?>

The index starts at 0 so that’s why we pass “1” to the function. For the first entry it would be “0”, the second is “1”, the third is “2” and so on…

Using the default WordPress functions

For a Single Meta Box

1
2
3
4
5
<?php 
$my_meta = get_post_meta( $post->ID, 'my_meta_name', true ); 
if( !empty( $my_meta[0]['my-number'] ) ) 
    echo 'Value:'.$my_meta[0]['my-number']; 
?>

For a Repeater Meta Box

To output all the “My Number” entries in the repeater field:

1
2
3
4
5
6
7
8
<?php 
$my_meta = get_post_meta( $post->ID, 'my_meta_name', true );
if( !empty( $my_meta ) ){
    foreach( $my_meta as $entry ){
        echo $entry['my-number'];
    }
}
?>

To output a specific “My Number” entry from the repeater field (e.g. the second entry):

1
2
3
4
5
<?php 
$my_meta = get_post_meta( $post->ID, 'my_meta_name', true );
if( !empty( $my_meta[1]['my-number'] ) )
    echo $my_meta[1]['my-number'];
?>

The index starts at 0 so that’s why we pass “1” to the function. For the first entry it would be “0”, the second is “1”, the third is “2” and so on…

After version 2.0 we can also get fields directly

1
2
3
4
5
6
<?php 
/* get the first entry */
$first_value = get_post_meta( $post->ID, 'my-number', true );
/* get the third entry */
$third_value = get_post_meta( $post->ID, 'my-number_2', true );
?>