Archived topic
Create shortcode
3 replies · Started by Sebastien on May 10, 2019
Hi,
I would like create a shortcode able to get the content of the field from the active post from where I use the shortcode.
For example, I use a custom field in my wp_post called "customfield".
Il would like to be able to write something like [customfield] in my text editor to be able to get the data stored in this field.
How can I manage that?
thanks
Hi there,
you could create a simple shortcode function like this:
function db_custom_field_shortcode() {
$custom_field = get_post_meta( get_the_ID(), 'your_custom_field_name', true );
$cf_content = '<p class="custom-class">' . $custom_field . '</p>';
return $cf_content;
}
add_shortcode( 'custom_field', 'db_custom_field_shortcode' );
Just change your_custom_field_name to match your custom field.
And the [custom_field] is the shortcode you add to your content.
awesome David, thanks !
To avoid to create 1 shortcode per custom field, would not be possible to automatize the function, and building a shortcode working like this : [custom_field: "your_custom_field_name"]
Maybe it is too much asking, I'm sorry then.
I'm learning as fast as possible ! and your plugin/theme/support helps me a lot.
Heres one someone created earlier:
https://www.isitwp.com/get-custom-field-value-with-shortcode/