【问题标题】:How to fix a shortcode to display its content in the correct location?如何修复简码以在正确的位置显示其内容?
【发布时间】: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');

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    短代码函数需要返回一些不回显文本的内容。

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

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

    function add_schema_person_shortcode() {
        ob_start();
    
        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>';
                    }
            }
    
        }
    
        $function_output = ob_get_contents();
    
        ob_end_clean();
    
        return $function_output;
    }
    add_shortcode( 'schema-person-shortcode', 'add_schema_person_shortcode');
    

    【讨论】:

    • 谢谢@Andrew Schultz。当我只有要显示的文本时,您的解决方案可以完美运行。我已经编辑了我的问题以显示完整的代码,其中有很多 php 函数。如何将其包装在$content 中?谢谢
    • @AtefWagih 我已经更新了我的答案以包含一个输出缓冲解决方案。只是为了让您知道未来的问题,请不要在收到答案后更改您的问题,因为您已经更改了要求的范围,这对于试图提供帮助的人来说是令人沮丧的。希望这能满足您的新要求。
    • 非常感谢@Andrew Schults。该解决方案完美运行。非常感谢,对编辑感到抱歉。下次我会考虑到这一点。
    • @AtefWagih 不是问题我只是让你知道其他人不会那么慷慨;)
    • 您介意看看这个问题吗,请:stackoverflow.com/questions/49621836/… 谢谢。
    猜你喜欢
    • 2010-11-12
    • 2016-09-08
    • 2015-08-25
    • 1970-01-01
    • 1970-01-01
    • 2019-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多