wordpress Elementor将相同的短代码显示两次(仅在编辑器上)

dgtucam1  于 2023-01-16  发布在  WordPress
关注(0)|答案(2)|浏览(215)

Elementor两次显示相同的简码。一次在放置简码的地方,另一次在页眉的正后方。问题只发生在编辑器中。在页面上,它显示正常。请帮助。我该如何修复它?

function display_grid_func ( $atts ){
echo '<div class="post-grid">';
    $args = array(
        'post_type' => 'realizacje',
        'post_status' => 'publish',
        'posts_per_page' => 9,
        'cat' => 5
    );

    $loop = new WP_Query($args);

    while ($loop->have_posts()) : $loop->the_post(); ?>

        <div class="post-tile home">
            <a href="<?php the_permalink(); ?>">
                <img src="<?php the_post_thumbnail_url('tile-foto'); ?>" />
            </a>
                <a class="button-zobacz home" href="<?php the_permalink(); ?>">Zobacz &#8594;</a>
            
        </div>

    <?php endwhile;
    wp_reset_postdata();?><?php
    echo '</div>';
}

add_shortcode('post_grid', 'display_grid_func');

到目前为止我已经尝试过:- 我改变了主题-我关闭了所有插件-我在谷歌上看

pgky5nke

pgky5nke1#

我解决了这个问题:)我使用了ob_start();并返回ob_get_clean();

function display_grid_func($atts)
{
    ob_start();
    echo '<div class="post-grid">';
    $args = array(
        'post_type' => 'realizacje',
        'post_status' => 'publish',
        'posts_per_page' => 9,
        'cat' => 5
    );

    $loop = new WP_Query($args);

    while ($loop->have_posts()) : $loop->the_post(); ?>

        <div class="post-tile home">
            <a href="<?php the_permalink(); ?>">
                <img src="<?php the_post_thumbnail_url('tile-foto'); ?>" />
            </a>
            <a class="button-zobacz home" href="<?php the_permalink(); ?>">Zobacz &#8594;</a>

        </div>

    <?php endwhile;
    wp_reset_postdata(); ?>
<?php echo '</div>';
    return ob_get_clean();
}

add_shortcode('post_grid', 'display_grid_func');
ewm0tg9j

ewm0tg9j2#

这个也解决了我的问题,谢谢

function elementor_shorcode_test() {
      ob_start();
        elementor_shorcode_test_output();
    return ob_get_clean();
}

相关问题