0

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 the plugin called 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>';

    }
}
2

You just need to render each section a little more efficiently. Making it look nice with all the fancy spacing won't pay off when a computer is reading the code (see tab v. space).

So for the 'Author' section, which you could apply to more sections, you want to gather all the date first and filter out any empty variables. Then check to see if you have data left. Once you're sure you have some data, then just loop through it to render it dynamically.

$author_info = array_filter( array (
    'honorificPrefix' => types_render_field( "schema-book-author-honorific-prefix", array () ),
    'givenName'       => types_render_field( "schema-book-author-given-name", array () ),
    'familyName'      => types_render_field( "schema-book-author-family-name", array () ),
    'honorificSuffix' => types_render_field( "schema-book-author-honorific-suffix", array () ),
) );

if ( ! empty( $author_info ) ) {

    echo '<tr><td>Author</td><td><span itemprop="author" itemscope itemtype="http://schema.org/Person">';

    foreach ( $author_info as $prop => $value ) {

        printf( '<span itemprop="%s">%s</span>', $prop, $value );

    }

    echo '</span></td></tr>';
}
  • Thanks @jgraup. It worked perfectly. I am not making the code pretty for the computer to read. I am making it readable for me, as I am still a beginner in coding. Much appreciated. – Atef Wagih Nov 24 '16 at 1:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.