Custom Fields
Re-Envisioned
Support General Outputing Field elements using Shortcode

This topic contains 3 replies, has 2 voices, and was last updated by  Anh Tran 1 year, 3 months ago.

  • Creator
    Topic
  • #8988

    quint_rabbit
    Participant

    Hi,
    sorry I am new to this and after several hours searching am still lost…

    I have created a fieldset_text using the Metabox builder:
    id “key_topic_1”

    which has the following keys for the elements within:
    topic
    obj1
    obj2

    I was trying code such as:
    [rwmb_meta options="topic" meta_key="key_topic_1"]
    OR
    [rwmb_meta key="topic" meta_key="key_topic_1"]
    but it always displays the whole fieldset.

    I would like to display not the whole fieldset, but just the content of individual keys (elements) of the fieldset using shortcodes in projects (pages) on my site.

    I need to insert the shortcodes into the html area of my pages (since I don’t want to modify the theme).

    Thanks in advance for explaining to a beginner…

Viewing 3 replies - 1 through 3 (of 3 total)
  • Author
    Replies
  • #9012

    Anh Tran
    Keymaster

    Hello,

    The shortcode only output the whole value of the custom field, in this case – the whole fieldset. In order to display only the values, you can create a custom shortcode to get and show the field value, like this:

    add_action( 'init', function () {
      add_shortcode( 'my_meta', function ( $atts ) {
        $atts   = shortcode_atts( [
          'meta_key' => '',
          'post_id'  => get_the_ID(),
        ], $atts );
        $values = rwmb_meta( $atts['meta_key'], array(), $atts['post_id'] );
        $output = '';
        foreach ( $values as $value ) {
          $output .= $value . ', ';
        }
    
        return $output;
      } );
    } );
    #9129

    quint_rabbit
    Participant

    Hi,
    to which file in WordPress do I need to add this code?
    How can I ensure that it will not be overwritten by automatic WordPress updates?

    #9146

    Anh Tran
    Keymaster

    You can insert that code into functions.php file of your theme or the main PHP file of your plugin. As it’s your theme, it’s not overwritten by WordPress.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Outputing Field elements using Shortcode’ is closed to new replies.