在内容顶部输出快捷码

时间:2016-06-02 作者:Ben H

我有一个短代码,它总是在页面内容的顶部输出。我不确定在这种情况下输出缓冲区的正确用法。

下面是我的快捷码函数:

function nt_course_note_call_sca() {

    // only show logged-in members on learn dash pages
    $post_type  = get_post_type();
    $types      = apply_filters( \'learndash_notes_supported_types\', array( \'sfwd-courses\', \'sfwd-topic\', \'sfwd-assignment\', \'sfwd-lessons\' ) );

    if ( is_user_logged_in() ){

        if( in_array( $post_type, $types ) ) {

          return nt_course_note_entry_field();

        }

    }

}

add_shortcode(\'reflective_learning\', \'nt_course_note_call_sca\');
nt\\u course\\u note\\u entry\\u字段的输出

//Prints Note field in front end and retieves exisintg note as placeholder
function nt_course_note_entry_field() {

    global $post;

    //ID\'s
    $current_user       = get_current_user_id();
    $current_lesson_id  = $post->ID;
    $current_post_type  = get_post_type();

    //Checks if note exists and changes title and body variables accordingly
    $args = array(
        \'post_type\'      => \'coursenote\',
        \'post_status\'   =>  array( \'draft\', \'publish\' ),
        \'meta_query\'     => array(
            //\'relation\' => \'AND\',
            array(
                \'key\'     => \'nt-note-current-lessson-id\',
                \'value\'   => $current_lesson_id,
                \'compare\' => \'=\',
            )
        ),
         \'author\' => $current_user
    );

    $the_query = new WP_Query( $args );

    if ($the_query->have_posts()){

        while ( $the_query->have_posts() ) : $the_query->the_post();

            //$title    = get_the_title();
            $body   = get_the_content();

            //var_dump( $body );

        endwhile;

     wp_reset_postdata();

    } else {

        //$title    = __( \'Note Title\', \'sfwd-lms\' );
        $body   = __( \'\', \'sfwd-lms\' );

    } 


    // get the course title or custom field if it exisists

if( get_field(\'reflective_learning_question\', $current_lesson_id) ) {

    $title  = get_field(\'reflective_learning_question\', $current_lesson_id);



    } else {
$title  = get_the_title($current_lesson_id);
        //$title    = __( \'Note Title\', \'sfwd-lms\' );
        //$body     = __( \'\', \'sfwd-lms\' );

    } 





    ?>

  <div id="nt_note_cont" class="note-container">



    <div class="nt-note-wrapper">

        <div class="note-header">

          <div class="note-header-title">
            <span class="nt-close-icon">x</span>
            <?php _e( \'Reflective Learning\', \'sfwd-lms\' ); ?>
          </div>

          <div class="note-header-actions"></div>

        </div> <!--/note-header-->

        <div id="apf-response"></div>

        <div class="note-body">

          <form id="nt-course-note" action="" method="post">

                <?php wp_nonce_field( basename(__FILE__), \'nt-course-note-nonce\') ?>
                <input type="text" name="nt-note-title hide-me" id="nt-note-title" value="<?php echo esc_attr( $title ); ?>" placeholder="" >

                <p name="nt-note-title" id="nt-note-title-display"><?php echo esc_attr( $title ); ?></p>


                <input type="hidden" name="nt-note-user-id" id="nt-note-user-id" value="<?php echo esc_attr( $current_user ); ?>">
                <input type="hidden" name="nt-note-current-lesson-id" id="nt-note-current-lessson-id" value="<?php echo esc_attr( $current_lesson_id ); ?>">
                <input type="hidden" name="nt-note-current-post-type" id="nt-note-current-post-type" value="<?php echo esc_attr( $current_post_type ); ?>">

                <textarea type="text" name="nt-note-body-custom" id="nt-note-body-custom" value="<?php echo $body; ?>" placeholder="" ><?php echo $body; ?></textarea>


                <?php

                $args = array(
                    \'media_buttons\'     =>      false,
                    \'textarea_name\'     =>      \'nt-note-body\',
                    \'editor_height\'     =>      175,
                    \'quicktags\'         =>      false,
                    \'teeny\'             =>      true,
                );

                add_filter( \'teeny_mce_buttons\', \'nt_tiny_mce_buttons\', 10, 2);
                wp_editor( $body, \'nt-note-body\', $args );
                remove_filter( \'teeny_mce_buttons\', \'nt_tiny_mce_buttons\' ); ?>

                <br>

                <input type="text" id="xyz" name="<?php echo apply_filters( \'honeypot_name\', \'date-submitted\') ?>" value="" style="display:none">

                <ul id="nt-note-actions">
                    <li><a href="#" class="learndash-notes-print-modal" data-note="<?php the_ID(); ?>"><i class="nticon-print"></i></a></li>
                    <li><a href="#" class="learndash-notes-download-modal" data-note="<?php the_ID(); ?>"><i class="nticon-file-word"></i></a></li>
                </ul>

                <input type="submit" id="nt-note-submit" value="<?php esc_attr_e( \'Save\', \'sfwd-lms\' ); ?>"/>

                <p class="nt-reset-dimensions"></p>

          </form>

        </div> <!--/.note-body-->

    </div> <!--/.nt-note-wrapper-->

  </div>
   <?php
}
我如何才能防止这种输出在页面顶部并以正确的位置输出?

1 个回复
最合适的回答,由SO网友:majick 整理而成

当有来自短代码的回显输出而不是返回结果时,就会发生这种情况。。。

我敢打赌nt_course_note_entry_field 很可能是回音而不是回音。如果有一个-或者您可以创建一个-使用类似的函数,如nt_get_course_note_entry_field 那没有回声。

如果不能更改函数,可以将其包装在输出缓冲区中并以这种方式返回结果,例如。

 ob_start();
 nt_course_note_entry_field();
 $result = ob_get_contents();
 ob_end_clean();
 return $result;

相关推荐

SHORTCODE_ATTS()中的$ATTS参数是什么?

这个WordPress developers reference page for shortcode_atts() 国家:$atts(array)(必选)用户在shortcode标记中定义的属性。但我不理解这个定义。例如,在WP Frontend Profile 插件:$atts = shortcode_atts( [ \'role\' => \'\', ], $atts ); 据我所知,shortcode\