Support » Plugin: Toolset Types - Custom Post Types, Custom Fields and Taxonomies » How to show/hide php table rows based on the content of custom fields

  • I have a website that displays books information.

    I want to add Schema.org microdata markup to the posts containing books.

    I created a set of custom fields using WP Types so I can add the required markup values in these fields.

    Then I created a function to show the information table in the single.php.

    My problem is that not every book has the same amount of microdata. (for example, some books are not translated, so they don’t have a translator). This leads to a poor outcome as the posts will display tables with lots of unfilled data.

    I want to modify the function to say this logic:

    if this custom field has value, echo this row
    then I can apply this modification to each row, so the end results will be a meaningful table each post, that contains only the available data.

    Is there a way to apply the logic above in my code?

    Here is the code I created:

    /* Schema.Org Book Microdata For Books Page Template */
    function add_schema_book_microdata () {
    if ( in_category('downloads') ) {
    echo '<div class="schema_book_microdata_container">';
                echo '<div itemscope itemtype="http://schema.org/Book">'; 
                    echo '<h3>'; echo 'Book Identification Card'; echo '</h3>';
                    echo '<table class="schema_book_microdata_table">';
                        echo '<tr>';
                            echo '<th class="schema-book-table-initial-column">'; echo 'Data'; echo '</th>';
                            echo '<th>';echo 'Details'; echo '</th>';
                        echo '</tr>';
                        echo '<tr>';
                            echo '<td>'; echo'Author'; echo '</td>';
                            echo '<td>';  
                                echo '<span itemprop="author" itemscope itemtype="http://schema.org/Person">';
                                echo'<span itemprop="honorificPrefix">'; echo types_render_field("schema-book-author-honorific-prefix", array( ) ); echo' </span>';
                                echo'<span itemprop="givenName">'; echo types_render_field( "schema-book-author-given-name", array( ) ); echo' </span>';
                                echo'<span itemprop="familyName">'; echo types_render_field("schema-book-author-family-name", array( ) ); echo' </span>'; 
                                echo'<span itemprop="honorificSuffix">'; echo types_render_field("schema-book-author-honorific-suffix", array( ) ); echo' </span>';
                                echo '</span>';
                            echo '</td>';
                        echo '</tr>';
                        echo '<tr>';
                            echo '<td>'; echo'Author Job Title'; echo '</td>';
                            echo '<td>';
                                echo '<span itemprop="author" itemscope itemtype="http://schema.org/Person">';
                                echo'<span itemprop="jobTitle">'; echo types_render_field("schema-book-author-job-title", array( ) ); echo'</span>';      
                                echo '</span>';
                            echo '</td>';
                        echo '</tr>';
                        echo '<tr>';
                            echo '<td>'; echo'Other Names for Author'; echo '</td>';
                            echo '<td>';
                                echo '<span itemprop="author" itemscope itemtype="http://schema.org/Person">';
                                echo'<span itemprop="additionalName">'; echo types_render_field("schema-book-author-additional-name", array( ) ); echo'</span>';      
                                echo '</span>';
                            echo '</td>';
                        echo '</tr>';
                        echo '<tr>';
                            echo '<td>'; echo'Translator'; echo '</td>';
                            echo '<td>';
                                echo '<span itemprop="translator" itemscope itemtype="http://schema.org/Person">';
                                echo'<span itemprop="honorificPrefix">'; echo types_render_field("schema-book-translator-honorific-prefix", array( ) ); echo' </span>';
                                echo'<span itemprop="givenName">'; echo types_render_field( "schema-book-translator-given-name", array( ) ); echo' </span>';
                                echo'<span itemprop="familyName">'; echo types_render_field("schema-book-translator-family-name", array( ) ); echo' </span>'; 
                                echo'<span itemprop="honorificSuffix">'; echo types_render_field("schema-book-translator-honorific-suffix", array( ) ); echo' </span>';
                                echo '</span>';
                            echo '</td>';
                        echo '</tr>';
                        echo '<tr>';
                            echo '<td>'; echo'Translation Organization'; echo '</td>';
                            echo '<td>';
                                echo '<span itemprop="translator" itemscope itemtype="http://schema.org/Organization">';
                                echo'<span itemprop="name">'; echo types_render_field( "schema-book-translator-organization", array( ) ); echo'</span>';      
                                echo '</span>';
                            echo '</td>';
                        echo '</tr>';
    
                        echo '<tr>';
                            echo '<td>'; echo'Editor'; echo '</td>';
                            echo '<td>';
                                echo '<span itemprop="editor" itemscope itemtype="http://schema.org/Person">';
                                echo'<span itemprop="honorificPrefix">'; echo types_render_field("schema-book-editor-honorific-prefix", array( ) ); echo' </span>';
                                echo'<span itemprop="givenName">'; echo types_render_field( "schema-book-editor-given-name", array( ) ); echo' </span>';
                                echo'<span itemprop="familyName">'; echo types_render_field("schema-book-editor-family-name", array( ) ); echo' </span>'; 
                                echo'<span itemprop="honorificSuffix">'; echo types_render_field("schema-book-editor-honorific-suffix", array( ) ); echo' </span>';
                                echo '</span>';
                            echo '</td>';
                        echo '</tr>';
                        echo '<tr>';
                            echo '<td>'; echo'Publisher'; echo '</td>';
                            echo '<td>';
                                echo '<span itemprop="publisher" itemscope itemtype="http://schema.org/Organization">';
                                echo'<span itemprop="name">'; echo types_render_field( "schema-book-publisher", array( ) ); echo'</span>';    
                                echo '</span>';
                            echo '</td>';
                        echo '</tr>';
                        echo '<tr>';
                            echo '<td>'; echo'Edition'; echo '</td>';
                            echo '<td>';
                                echo '<span itemprop="bookEdition">'; echo types_render_field("schema-book-edition", array( ) ); 
                                echo '</span>';
                            echo '</td>';
                        echo '</tr>';
                        echo '<tr>';
                            echo '<td>'; echo'No. of Pages'; echo '</td>';
                            echo '<td>';
                                echo '<span itemprop="numberOfPages">'; echo types_render_field("schema-book-number-of-pages", array( ) ); 
                                echo '</span>';
                            echo '</td>';
                        echo '</tr>';
                        echo '<tr>';
                            echo '<td>'; echo'Date Published'; echo '</td>';
                            echo '<td>';
                                echo '<span itemprop="datePublished">'; echo types_render_field("schema-book-publish-date", array( ) ); 
                                echo '</span>';
                            echo '</td>';
                        echo '</tr>';
                        echo '<tr>';
                            echo '<td>'; echo'ISBN/ISSN'; echo '</td>';
                            echo '<td>';
                                echo '<span itemprop="isbn">'; echo types_render_field("schema-book-isbn-issn", array( ) ); 
                                echo '</span>';
                            echo '</td>';
                        echo '</tr>';
                    echo '</table>';
        echo '</div>';
    
        }
    }
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Beda

    (@bedas)

    There certainly is, but it is not covered in this Support Forum.
    Here we handle issues surging from the usage of types, and PHP coding is not one of them.

    What you need is to use else/if statements in your PHP Code, and compare the values of the get_post_meta or types_render_field functions to your desired values
    Then depending on the outcome, you display or not the Fields, or rows.

    I would suggest looking at both functions here:
    http://www.w3schools.com/php/php_if_else.asp
    http://php.net/manual/de/control-structures.elseif.php

    Hi Beda,
    thanks for the reply.

    I totally understand that it needs to be done using the if/else php function.
    I just can’t target the WP Types fields directly to say:
    if this wp type field is not empty, do this…etc

    Plugin Support Beda

    (@bedas)

    You already successfully got the Fields, right?
    I see you use types_render_field and that is just what you need.

    Populate a $variable or just use it in the condition.

    Let’s say:

    $value = types_render_field( 'field-slug', array($args) );
    if ($value == 'whatever'){
    //magic to happen when above is true
    }
    else {
    //etc
    }
    

    Or you can simply compare the types_render_fields to empty PHP function.
    http://php.net/manual/de/function.empty.php

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to show/hide php table rows based on the content of custom fields’ is closed to new replies.