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>';
}
}