【发布时间】:2018-03-22 14:18:46
【问题描述】:
我正在尝试创建一个短代码,它将根据 Schema.org Person Type.
我创建了这个简单的测试短代码,它会回显一个短语。短语是:Personal Info。代码如下:
function add_schema_person_shortcode() {
echo '<div class="schema_person_microdata_container">';
echo '<div itemscope itemtype="http://schema.org/Person">';
echo '<h3>'; echo 'Personal Info'; echo '</h3>';
echo '</div>';
echo '</div>';
}
add_shortcode( 'schema-person-shortcode', 'add_schema_person_shortcode');
我的问题是短语Personal Info 显示在所有内容的顶部,即使短代码上方有内容。
Please refer to this test post by clicking here.
帖子内容是:
<p>this is text written ABOVE the shortcode.</p>
[schema-person-shortcode]
<p>this is text written below the shortcode.</p>
由于我没有任何特殊代码来强制短代码内容显示在页面顶部,因此我无法确定错误在哪里。
如果有人能指出如何解决此问题并让简码内容显示在正确位置,我将不胜感激。
提前致谢。
编辑
我从下面的@Andrew Schultz 那里得到了很好的回答,但它仅在我想显示文本时才对我有用。为了简化问题,我在原始问题中添加了一个虚拟代码。
但是,我的完整代码包含 if 语句,因此我将完整代码粘贴在下面,以帮助在代码中有 if 语句时获得解决方案。
谢谢。
function add_schema_person_shortcode() {
if (ICL_LANGUAGE_CODE == 'ar') {
if( function_exists( 'types_render_field' ) ){
$author_info = array_filter( array (
'honorificPrefix' => types_render_field( "schema-person-honorific-prefix", array () ),
'givenName' => types_render_field( "schema-person-given-name", array () ),
'familyName' => types_render_field( "schema-person-family-name", array () ),
'honorificSuffix' => types_render_field( "schema-person-honorific-suffix", array () ),
) );
$author_additional_name = array_filter( array (
'additionalName' => types_render_field( "schema-person-additional-name", array () ),
) );
$person_monastic_name = array_filter( array (
'additionalName' => types_render_field( "schema-person-monastic-name", array () ),
) );
$person_date_of_birth = array_filter( array (
'birthDate' => types_render_field( "schema-person-date-of-birth", array () ),
) );
$person_place_of_birth = array_filter( array (
'birthPlace' => types_render_field( "schema-person-place-of-birth", array () ),
) );
if ( ! empty( $author_info ) ) {
echo '<div class="schema_person_microdata_container">';
echo '<div itemscope itemtype="http://schema.org/Person">';
echo '<h3>'; echo 'كارت التعريف بالشخصية'; echo '</h3>';
echo '<table class="schema_book_microdata_table">';
echo '<tr>';
echo '<th class="schema-book-table-initial-column">'; echo 'البيانات'; echo '</th>';
echo '<th>';echo 'التفاصيل'; echo '</th>';
echo '</tr>';
echo '<tr><td>الأسم</td><td>';
foreach ( $author_info as $prop => $value ) {
printf( '<span itemprop="%s"> %s </span>', $prop, $value );
}
echo '</span></td></tr>';
if ( ! empty( $author_additional_name ) ) {
echo '<tr><td>الأسم قبل الرسامة</td><td>';
foreach ( $author_additional_name as $prop => $value ) {
printf( '<span itemprop="%s"> %s </span>', $prop, $value );
}
echo '</span></td></tr>';
}
if ( ! empty( $person_monastic_name ) ) {
echo '<tr><td>الأسم الرهباني</td><td>';
foreach ( $person_monastic_name as $prop => $value ) {
printf( '<span itemprop="%s"> %s </span>', $prop, $value );
}
echo '</span></td></tr>';
}
if ( ! empty( $person_date_of_birth ) ) {
echo '<tr><td>تاريخ الميلاد</td><td>';
foreach ( $person_date_of_birth as $prop => $value ) {
printf( '<span itemprop="%s"> %s </span>', $prop, $value );
}
echo '</span></td></tr>';
}
if ( ! empty( $person_place_of_birth ) ) {
echo '<tr><td>محل الميلاد</td><td>';
foreach ( $person_place_of_birth as $prop => $value ) {
printf( '<span itemprop="%s"> %s </span>', $prop, $value );
}
echo '</span></td></tr>';
}
echo '</table>';
echo '</div>';
echo '</div>';
}
}
}
}
add_shortcode( 'schema-person-shortcode', 'add_schema_person_shortcode');
【问题讨论】: