[Resolved] Create shortcode

Home Forums Support [Resolved] Create shortcode

Home Forums Support Create shortcode

  • This topic has 3 replies, 2 voices, and was last updated 4 years ago by David.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
  • #895029

    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?


    Customer Support

    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.

    Customer Support
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.