我有一个显示书籍信息的网站。
我想添加Schema.org
包含书籍的帖子的微数据标记。
我使用名为WP Types
因此,我可以在这些字段中添加所需的标记值。
然后,我创建了一个函数来显示single.php
.
我的问题是,并非每本书都有相同数量的微观数据。(例如,有些书没有翻译,所以他们没有翻译)。这会导致糟糕的结果,因为帖子将显示包含大量未填充数据的表格。
我想修改该函数以表示以下逻辑:
if this custom field has value, echo this row
然后,我可以将此修改应用于每一行,因此最终结果将是每个帖子的一个有意义的表,其中只包含可用数据。
有没有办法在我的代码中应用上述逻辑?
以下是我创建的代码:
/* 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>\';
}
}
最合适的回答,由SO网友:jgraup 整理而成
您只需要更高效地渲染每个部分。当计算机正在读取代码时,使用所有花哨的间距使其看起来好看是没有用的(请参见tab v.space)。
因此,对于“Author”部分,您可以将其应用于更多的部分,您希望首先收集所有日期并过滤掉任何空变量。然后检查是否还有数据。一旦你确定你有一些数据,然后循环通过它来动态渲染它。
$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>\';
}