Title: Custom ACF inside Shortcode
Last modified: September 4, 2019

---

# Custom ACF inside Shortcode

 *  Resolved [maurodimarino](https://wordpress.org/support/users/maurodimarino/)
 * (@maurodimarino)
 * [6 years, 9 months ago](https://wordpress.org/support/topic/custom-acf-inside-shortcode/)
 * Hello, it’s a great plugin!
    I would like to know if there is a way to print 
   a custom field like an image inside the shortcode or another caption at the end
   of the table. Thank you!

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

 *  Plugin Author [Johann Heyne](https://wordpress.org/support/users/jonua/)
 * (@jonua)
 * [6 years, 9 months ago](https://wordpress.org/support/topic/custom-acf-inside-shortcode/#post-11901205)
 * The HTML specs say that you can specify only one caption per table.
 * For shortcodes in table cells, you can use the WordPress function do_shortcode()
   [https://developer.wordpress.org/reference/functions/do_shortcode/](https://developer.wordpress.org/reference/functions/do_shortcode/)
   in the template code like…
    `echo do_shortcode( $th['c'] );`
 * Cheers,
    Johann
    -  This reply was modified 6 years, 9 months ago by [Johann Heyne](https://wordpress.org/support/users/jonua/).
 *  Thread Starter [maurodimarino](https://wordpress.org/support/users/maurodimarino/)
 * (@maurodimarino)
 * [6 years, 9 months ago](https://wordpress.org/support/topic/custom-acf-inside-shortcode/#post-11901598)
 * Thank you for your answer!
    I think I was not clear and sorry for my english.
 * I have created in ACF the table field then i have created a custom post type 
   like “Super Tables”
 * Then I have inserted in the function your code for output the table trought shortcode
   where I want.
 * Now I would like to create my custom field (ES. Text Field) to match the same
   shortcode of the table.
 * Is that possibile?
    Thank you
 *  Plugin Author [Johann Heyne](https://wordpress.org/support/users/jonua/)
 * (@jonua)
 * [6 years, 9 months ago](https://wordpress.org/support/topic/custom-acf-inside-shortcode/#post-11901779)
 * I´m not sure, if I understand your needs. You have a shortcode, that outputs 
   a table from a table fields of an custom post type. But then I´m lost.
 *  Thread Starter [maurodimarino](https://wordpress.org/support/users/maurodimarino/)
 * (@maurodimarino)
 * [6 years, 9 months ago](https://wordpress.org/support/topic/custom-acf-inside-shortcode/#post-11902735)
 * I have inserted this code in the functions.php
 *     ```
       function shortcode_acf_tablefield( $atts ) {
   
           $a = shortcode_atts( array(
               'table-class' => '',
               'field-name' => false,
               'post-id' => false,
           ), $atts );
   
           $table = get_field( $a['field-name'], $a['post-id'] );
   
   
           $return = '';
   
   
           if ( $table ) {
   
               $return .= '<table class="super-table ' . $a['table-class'] . '" border="0">';
   
   
   
   
   
   
                   if ( ! empty( $table['caption'] ) ) {
   
                       echo '<div class="titolo-tabella">' . $table['caption'] . '</div>';
                   }
   
                   if ( $table['header'] ) {
   
                       $return .= '<thead>';
   
                           $return .= '<tr>';
   
                               foreach ( $table['header'] as $th ) {
   
                                   $return .= '<th>';
                                       $return .= $th['c'];
                                   $return .= '</th>';
                               }
   
                           $return .= '</tr>';
   
                       $return .= '</thead>';
                   }
   
   
   
                   $return .= '<tbody>';
   
                       foreach ( $table['body'] as $tr ) {
   
                           $return .= '<tr>';
   
                               foreach ( $tr as $td ) {
   
                                   $return .= '<td>';
                                       $return .= do_shortcode( $td['c'] );
                                   $return .= '</td>';
                               }
   
                           $return .= '</tr>';
                       }
   
                   $return .= '</tbody>';
   
               $return .= '</table>';
           }
   
           return $return;
       }
   
       add_shortcode( 'table', 'shortcode_acf_tablefield' );
       ```
   
 * Then I created a custom post type where I just create a table and with this shortcode:
   [
   table field-name=”super_table” post-id=”9″ table-class=”my-table”] I output the
   table where I want.
 * Now I want to create a new custom field so that it can be used together with 
   the table. For example a new title or a new image in addition to the caption
   
   Is that cleare? Thank you for your patience
 *  Plugin Author [Johann Heyne](https://wordpress.org/support/users/jonua/)
 * (@jonua)
 * [6 years, 9 months ago](https://wordpress.org/support/topic/custom-acf-inside-shortcode/#post-11902793)
 * Just get any field by the same post-id of the table like…
 * `$caption_text = get_field( 'caption-field-name', $a['post-id'] );`
 *  Thread Starter [maurodimarino](https://wordpress.org/support/users/maurodimarino/)
 * (@maurodimarino)
 * [6 years, 9 months ago](https://wordpress.org/support/topic/custom-acf-inside-shortcode/#post-11902924)
 * Thank you very much!! It worked like a charm!

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

The topic ‘Custom ACF inside Shortcode’ is closed to new replies.

 * ![](https://ps.w.org/advanced-custom-fields-table-field/assets/icon-256x256.png?
   rev=1962986)
 * [Table Field Add-on for ACF and SCF](https://wordpress.org/plugins/advanced-custom-fields-table-field/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/advanced-custom-fields-table-field/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/advanced-custom-fields-table-field/)
 * [Active Topics](https://wordpress.org/support/plugin/advanced-custom-fields-table-field/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/advanced-custom-fields-table-field/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/advanced-custom-fields-table-field/reviews/)

 * 6 replies
 * 2 participants
 * Last reply from: [maurodimarino](https://wordpress.org/support/users/maurodimarino/)
 * Last activity: [6 years, 9 months ago](https://wordpress.org/support/topic/custom-acf-inside-shortcode/#post-11902924)
 * Status: resolved