Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Johann Heyne

    (@jonua)

    The code example to get a table field′s data and rendering the table has to be placed in a template. The ACF field must, of course, be available for this template.

    Plugin Author Johann Heyne

    (@jonua)

    Sorry for my delay,

    the code example is for use in a template like page.php or post.php, not functions.php. You get the table data just like other ACF field′s with $table_data = get_field( 'your_table_field name' );. Use my code example to render the table.

    Cheers,
    Johann

    FOX

    (@flyingfox)

    Is there a way to paste tables with a shortcode to a post? This is what I need. Thx for help. The ACF Shortcode does not work.

    kind regards R.

    Plugin Author Johann Heyne

    (@jonua)

    Hi flyingfox,

    there is a way, to paste tables with a shortcode in a post. But this is not a default functionality of the plugin. You can integrate this yourself. Just keep reading.

    First of all the explanation, why the ACF shortcode does not work for tables or other data driven fields. The ACF shortcode only works for simple text based values. It just returns the data like get_field(). For fields such as repeaters or tables, the content is provided as an array or object for further processing into HTML. So the implementation as HTML is something you have to do by your own.

    First step to get a shortcode, that returns a HTML table. You need a function in your functions.php file to get the html by providing a tables data. The following code does that:

    if ( ! function_exists( 'render_table' ) ) {
    
        function render_table( $data = false ) {
    
            $return = '';
    
            if ( $data ) {
    
                $return .= '<table border="0">';
    
                    if ( isset( $data['header'] ) and is_array( $data['header'] ) ) {
    
                        $return .= '<thead>';
    
                            $return .= '<tr>';
    
                                foreach ( $data['header'] as $th ) {
    
                                    $return .= '<th>';
                                        $return .= $th['c'];
                                    $return .= '</th>';
                                }
    
                            $return .= '</tr>';
    
                        $return .= '</thead>';
                    }
    
                    $return .= '<tbody>';
    
                        if ( isset( $data['body'] ) ) {
    
                            foreach ( $data['body'] as $tr ) {
    
                                $return .= '<tr>';
    
                                    foreach ( $tr as $td ) {
    
                                        $return .= '<td>';
                                            $return .= $td['c'];
                                        $return .= '</td>';
                                    }
    
                                $return .= '</tr>';
                            }
    
                        }
    
                    $return .= '</tbody>';
    
                $return .= '</table>';
            }
    
            return $return;
        }
    }

    Change that code for your needs.

    Second step. Lets provide a function that combines get_field() and render_table():

    if ( ! function_exists( 'get_table' ) ) {
    
        function get_table( $name = false, $post_id = false ) {
    
            if ( ! function_exists( 'get_field' ) OR ! function_exists( 'render_table' ) ) {
    
                return;
            }
    
            if ( $name ) {
    
                $data = get_field( $name, $post_id, true );
                return render_table( $data );
            }
        }
    }

    Now you could use <?php echo get_table( 'your_table_field_name' ); ?> in a template to output the tables html of a table field. You also could output a table fields html of another page by providing a post ID like <?php echo get_table( 'your_table_field_name', 123 ); ?>.

    The last step is to define a shortcode in functions.php, that uses the get_table() function…

    function shortcode_acf_tablefield( $atts ) {
    
        $a = shortcode_atts( array(
            'name' => false,
            'post-id' => false,
        ), $atts );
    
        return get_table( $a['name'], $a['post_id'] );
    }
    
    add_shortcode( 'table', 'shortcode_acf_tablefield' );

    Now you can use the shortcode like [table name="your_table_field_name"] or [table name="your_table_field_name" post_id="123"].

    Cheers,
    Johann

    • This reply was modified 6 years, 12 months ago by Johann Heyne.
    • This reply was modified 6 years, 12 months ago by Johann Heyne.
    • This reply was modified 6 years, 12 months ago by Johann Heyne.

    Brilliant, thanks Johann. You saved my day.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Where add this codes?’ is closed to new replies.