首页 > 解决方案 > 短代码出现在内容顶部

问题描述

我目前正在尝试向我的 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');

标签: phphtmlwordpressshortcodewordpress-shortcode

解决方案


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

尝试:

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

推荐阅读