短代码不起作用。返回错误消息

问题描述 投票:0回答:1

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

[注意:我也尝试在启动html之前关闭php标签并在结束html后再次启动它]

Parse error: syntax error, unexpected '<' in C:\xampp\htdocs\theme-dev\wp-content\themes\sportify\functions.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' );
wordpress shortcode
1个回答
0
投票

错误是因为你试图在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' );
© www.soinside.com 2019 - 2024. All rights reserved.