Support » Plugin: Advanced Custom Fields: Table Field » Only show table if it has content?

  • Resolved weblicity

    (@weblicity)


    the standard

    <?php if( get_field('name_of_table_field') ) {
    echo 'here is the table';
    }?>

    doesn’t seem to work, it just always shows the table (even if it has no content) and it’s layout, but without content. I’m using WPML to copy the fields to another language and then empty it if it isn’t needed in that language, perhaps that’s part of the issue.

    • This topic was modified 8 months, 1 week ago by  weblicity.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Johann Heyne

    (@jonua)

    Hi,

    get_field() returns NULL, if the table has no content. So you can use…

    if ( get_field( 'table_classic_editor' ) !== NULL ) {
        echo 'here is the table';
    }

    or more general if the result is one of the following NULL, FALSE or empty string ''

    if ( ! empty( get_field( 'table_classic_editor' ) ) ) {
        echo 'here is the table';
    }

    WPML should not be part of that problem.

    Cheers,
    Johann

    • This reply was modified 8 months, 1 week ago by  Johann Heyne.
    • This reply was modified 8 months, 1 week ago by  Johann Heyne.
    • This reply was modified 8 months, 1 week ago by  Johann Heyne.
    • This reply was modified 8 months, 1 week ago by  Johann Heyne.
    Plugin Author Johann Heyne

    (@jonua)

    Hi,

    I was wrong. For some reason I´ve got NULL using get_field(). But it should be FALSE on an empty table so that your code example should work.

    I´m going to investigate that.

    Cheers,
    Johann

    Plugin Author Johann Heyne

    (@jonua)

    OK, get_field() returns NULL, if the requested field is not saved in the database. If there is an empty table saved in the database, get_field() returns FALSE. So using

    if ( ! empty( get_field( 'my-table' ) ) { 
    }

    ist the way, that should work.

    Cheers,
    Johann

    • This reply was modified 8 months, 1 week ago by  Johann Heyne.

    Hi Johann,

    Thanks for your reply. It still didn’t work, but it was my mistake.
    I use the table in a custom post type, and copied the first post and edited the information, so that the headers of the table will always be the same.
    I just emptied the table’s content without removing the columns and rows.
    Obviously, that doesn’t count as it being empty or null.

    So, a table is only empty if it doesn’t have any cells or rows.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Only show table if it has content?’ is closed to new replies.