简码不起作用。返回误差测量



我有此代码用于制作自定义快捷代码。但是它返回此错误消息。如何克服这个错误。

[注意:我还尝试在启动HTML之前关闭PHP标签,并在结束HTML之后再次启动它]

Parse error: syntax error, unexpected '<' in C:xampphtdocstheme-devwp-contentthemessportifyfunctions.php on line 77

// Add Shortcode
    function custom_shortcode() {
    <div class="intro"> /* line no 79 */
        <div class="intro_boxes_wrap">
            <div class="d-flex flex-row align-items-start justify-content-start flex-wrap">
                <?php 
                    $query = new WP_Query(array(
                        'post_type'     => 'intro',
                        'post_per_page' => 3,
                    ));
                    while($query->have_posts()): $query->the_post(); 
                ?>
                <!-- Intro Box -->
                <div class="intro_box d-flex flex-column align-items-center justify-content-center text-center">
                    <?php the_post_thumbnail(); ?>
                    <div class="intro_box_title"><h3><?php the_title(); ?></h3></div>
                    <div class="intro_box_text">
                        <?php echo get_the_content(); ?>
                    </div>
                </div>
                <?php endwhile; wp_reset_query();?>
            </div>
        </div>
    </div>
}
add_shortcode( 'kollol', 'custom_shortcode' );

错误是因为您尝试在PHP块中输出HTML。您必须使用php end/start标签

// Add Shortcode
    function custom_shortcode() {
ob_start();
?>
<div class=intro> ... </div> //... exactly the code you got now 
<?php
return ob_get_clean();  //ob_get_clean() is returning everything you echoed or rendered since ob_start()
}
add_shortcode( 'kollol', 'custom_shortcode' );

最新更新