Support » Plugin: Advanced Custom Fields: Table Field » Shortcode conditional

  • Resolved nanny7

    (@nanny7)


    Hi I am using this:

    
    function shortcode_acf_tablefield( $atts ) {
    	
        $a = shortcode_atts( array(
            'table-class' => '',
            'field-name' => false,
            'post-id' => false,
        ), $atts );
    $tables = (get_field( 'add_members_table' ) == 'yes');
    
    if ( ! empty ( $tables ) ) {
    
        $table = get_field( $a['field-name'], $a['post-id'] );
    	$caption_text = get_field( 'table_name', $a['post-id'] );
    
        $return = '';
    
        if ( $table ) {
    		 $return .= '<h2>' . $caption_text . '</h2>';
    
            $return .= '<table class="' . $a['table-class'] . '" border="0">';
    
                if ( ! empty( $table['caption'] ) ) {
    
                    echo '<caption>' . $table['caption'] . '</caption>';
                }
    
                if ( $table['header'] ) {
    
                    $return .= '<thead>';
    
                        $return .= '<tr>';
    
                            foreach ( $table['header'] as $th ) {
    
                                $return .= '<th scope="col">';
                                    $return .= $th['c'];
                                $return .= '</th>';
                            }
    
                        $return .= '</tr>';
    
                    $return .= '</thead>';
                }
    
                $return .= '<tbody>';
    
                    foreach ( $table['body'] as $tr ) {
    
                        $return .= '<tr>';
    						 $i = 0;
                            foreach ( $tr as $td ) {
    
                                $return .= '<td data-column="' . $table['header'][$i]['c'] . ':">';
                                    $return .= $td['c'];
                                $return .= '</td>';
    							$i++;
                            }
    
                        $return .= '</tr>';
                    }
    
                $return .= '</tbody>';
    
            $return .= '</table>';
        }
    
        return $return;
    }
    
    }
    
    add_shortcode( 'tablefield', 'shortcode_acf_tablefield' );
    

    But I am wanting to add an addition to the conditional if they have not selected the table but want to upload a pdf etc. instead. I have the buttons in acf working correctly and if the field is “no” the table doesn’t show.

    I am trying to add this as an else

    
    $pdf = (get_field( 'add_members_table' ) == 'no');
    if ( ! empty ( $pdf ) ) {
    $content_type = get_field('choose_which_file_to_show');
        if ($content_type == 'PDF') { ?>
     <img class="align-middle" src="<?php echo get_stylesheet_directory_uri(); ?>/img/pdf.png" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" width="32" height="32" /> <a href="<?php the_field('upload_pdf'); ?>" ><?php the_title(); ?></a>
     <?php
    } 
    elseif ($content_type == 'XLS') { ?>
    <img class="align-middle" src="<?php echo get_stylesheet_directory_uri(); ?>/img/xls.png" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" width="32" height="32" /> <a href="<?php the_field('upload_excel_sheet'); ?>" ><?php the_title(); ?></a>
    }
      <?php 
    }
    

    but it won’t show up even if I do another shortcode. Is there a way to add it within the shortcode or will it break?

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Shortcode conditional’ is closed to new replies.