[Resolved] Create shortcode

Home Forums Support [Resolved] Create shortcode

Home Forums Support Create shortcode

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #895029
    Sebastien

    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

    #895056
    David
    Staff
    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.

    #895111
    Sebastien

    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.

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