Function — Add custom fields for product data grid - martindubenet/Wordpress GitHub Wiki
A Custom Field is composed of a key
and a value
metadatas stored in the database and associated with a post type. You only need to create a new key
once, after which you can assign a value
to that key for every post (of the same type), if you so desire. You can also assign more than one value
to a key
.
- WordPress Custom Fields 101: Tips, Tricks, and Hacks
- Adding WooCommerce custom fields programmatically
- Edit the post or page,
- Click on the vertical ellipsis « ⋮ » button (located at the top of the right sidebar of the screen),
- From the contextual menu, click « Options »,
- From the « Options » modal window,
- Check the ✅ « Custom fields » option located the Advanced panels section,
- Click « Enable & Reload » button.
- This will enable the Custom Fields widget located under the main (Gutenberg) text editor area.
Use the_meta()
function to display a list of post custom fields.
Note : This function will ignore meta_keys that begin with an underscore (
_*
).
function the_meta_dl() {
$keys = get_post_custom_keys();
if ( $keys ) {
$dl_html = '';
foreach ( (array) $keys as $key ) {
$keyt = trim( $key );
if ( is_protected_meta( $keyt, 'post' ) ) {
continue;
}
$values = array_map( 'trim', get_post_custom_values( $key ) );
$value = implode( ', ', $values );
$html = sprintf(
'<dl class="meta-set"><dt class="post-meta-key">%s</dt><dd>%s</dd></dl>\n',
/* translators: %s: Post custom field name. */
sprintf( _x( '%s:', 'Post custom field name' ), $key ),
$value
);
/**
* Filters the HTML output of the definition list element within the post custom fields section.
*
* @since 2.2.0
*
* @param string $html The HTML output for the li element.
* @param string $key Meta key.
* @param string $value Meta value.
*/
$dl_html .= apply_filters( 'the_meta_key', $html, $key, $value );
}
if ( $dl_html ) {
echo '<section class="post-meta-list">\n{$li_html}</section>\n';
}
}
}
mgibbs189.github.io/custom-field-suite/
Available for FREE via on GitHub or Wordpress.org
Code exemple for your theme page: <?php echo CFS()->get( 'your_field_name' ); ?>
It is highly recommanded to use « Advanced Custom Fields » plugin to manage custom fields in your site. See a bunch of code examples.
-
ACF Documentations :
- Displaying CF values within your theme,
- Multilingual Custom Fields supports WPML.
- Functions :
-
get_field_object()
to display thekey
and/orvalue
within your theme page.
-
- Guide :
- Creating a new field type using the starter kit ACF Field Type Template from Github.
- Dynamically populate a select field’s choices
- ACF Child Plugins :