短代码出现在内容顶部

时间:2021-02-28 12:54:17

标签: php html wordpress shortcode wordpress-shortcode

我目前正在尝试向我的 Wordpress 网站添加一个短代码,但目前它显示在页面顶部,而不是我放置它的位置。

除此问题外,一切正常。


    // function that runs when shortcode is called
    function testimonial_short() { 
        if( get_query_var( 'page' ) ) { 
            $page = get_query_var( 'page' ); 
        } else { 
            $page = 1; 
        }
        $args = array(
            'post_type' => 'drivers',
            'posts_per_page' => 4,
            'paged' => $page
        );
        $products = new WP_Query( $args );
        ?>
        <div class="owl-carousel owl-theme">
        <div class="testimonial-outer">
                <div class="image-col">
                <img class="testimonial-img" src="<?php echo get_field('image');?>">
                </div>
                <!-- image-col -->
                <div class="text-col">
                <h4>
                    <?php echo get_field('name');?>
                </h4>
                <p>
                    <?php echo get_field('testimonial');?>
                </p>
                </div>
                <!-- text-col -->
            </div>
            <!-- testimonial-outer -->
        </div>
        <?php
    } 
    // register shortcode
    add_shortcode('testimonial', 'testimonial_short');

1 个答案:

答案 0 :(得分:2)

您的 testimonial_short 函数没有返回 HTML 而是立即输出它。这导致它出现在页面顶部,因为在尚未发送任何内容的情况下对短代码进行评估。

试试:

function testimonial_short(){
    ...
    return "<div>my html</div>";
}