Support » Plugin: Advanced Custom Fields: Table Field » Custom ACF inside Shortcode

  • Resolved maurodimarino

    (@maurodimarino)


    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

    (@jonua)

    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/ in the template code like…
    echo do_shortcode( $th['c'] );

    Cheers,
    Johann

    • This reply was modified 2 months, 2 weeks ago by Johann Heyne.

    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

    (@jonua)

    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.

    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

    (@jonua)

    Just get any field by the same post-id of the table like…

    $caption_text = get_field( 'caption-field-name', $a['post-id'] );

    Thank you very much!! It worked like a charm!

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.