如何修复短代码以在正确的位置显示其内容?

时间:2018-03-21 03:37:42

标签: php wordpress

我正在尝试创建一个短代码,它将显示一些基于的信息  Schema.org Person Type

我创建了这个简单的测试短代码,它将回显一个短语。这句话是: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

我的问题是,即使短代码上方有内容,短语<p>this is text written ABOVE the shortcode.</p> [schema-person-shortcode] <p>this is text written below the shortcode.</p> 也会显示在所有内容之上。

Please refer to this test post by clicking here.

帖子内容为:

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

但是,前端显示的是: enter image description here

由于我没有任何特殊代码强制短信代码内容显示在页面顶部,我无法确定错误的位置。

如果有人能指出我如何解决这个问题并让短代码内容显示在正确的位置,我会感激不尽。

提前致谢。

修改

下面我从@Andrew Schultz那里得到了一个很好的答案,但只有当我想显示文字时它才适合我。我在原始问题中添加了一个虚拟代码,以简化操作。

但是,我的完整代码包含if语句,所以我粘贴下面的完整代码,以帮助在代码中有if语句时获得解决方案。

感谢。

let stripped = returnData!.replacingOccurrences(of: "\n", with: " ")
var inputString = stripped.components(separatedBy: " ").dropFirst(2)
inputString = inputString.dropLast()

1 个答案:

答案 0 :(得分:2)

短代码功能需要返回不回显文本的东西。

1. Id
2. created date
3. created time
4. start date
5. end date

这里我举例说明如何使用PHP输出缓冲区收集所有的echo输出然后捕获它并在函数结束时返回它。

function add_schema_person_shortcode() {
    $content = '<div class="schema_person_microdata_container">';
    $content .= '<div itemscope itemtype="http://schema.org/Person">'; 
    $content .= '<h3>Personal Info</h3>';
    $content .= '</div>';
    $content .= '</div>';

    return $content;
}

add_shortcode( 'schema-person-shortcode', 'add_schema_person_shortcode');