When I add a custom shortcode that displays recent posts of custom post type, it renders fine the fist time in preview. But when close and open the preview again, Elementor renders it two times.
In the following screenshot, I have added the shorcode once but it rendered twice. Only the second one is selectable in the preview.
Steps to reproduce:
Environment
WordPress v4.9.1
Elementor: 1.9.1
Theme: twentyseventeen (with custom shortcode and css tweaks).
Plugins: Only Elementor is installed and activated.
@rahulnever2far
I can't replicate,
please paste your shortcode and I'll have a look.
I figured out what caused that issue. The shortcodes are supposed to return and I was echoing markup. Echoing made Elementor display the output of the shortcode twice in the preview mode.
Here's a very simplified version of my shourtcode
add_shortcode( 'elementor_shorcode_test', 'elementor_shorcode_test' );
function elementor_shorcode_test() {
elementor_shorcode_test_output();
}
function elementor_shorcode_test_output() {
$elementor_shorcode_test_query = new WP_Query( array(
'posts_per_page' => 4,
) );
if ( $elementor_shorcode_test_query->have_posts() ) {
while ( $elementor_shorcode_test_query->have_posts() ) {
$elementor_shorcode_test_query->the_post();
the_title();
}
}
}
Buffering output in elementor_shorcode_test()
fixed the issue.
function elementor_shorcode_test() {
ob_start();
elementor_shorcode_test_output();
return ob_get_clean();
}
I'm not sure if we still consider this an issue. On one hand, shortcodes are supposed to return (not echo). On another hand echoing creates the issue in the Elementor Preview but it works fine everywhere else.
Not an issue with Elementor, its an issue with the shortcode, a shortcode should return the content and not echo it out.
Most helpful comment
I figured out what caused that issue. The shortcodes are supposed to return and I was echoing markup. Echoing made Elementor display the output of the shortcode twice in the preview mode.
Here's a very simplified version of my shourtcode
Buffering output in
elementor_shorcode_test()
fixed the issue.I'm not sure if we still consider this an issue. On one hand, shortcodes are supposed to return (not echo). On another hand echoing creates the issue in the Elementor Preview but it works fine everywhere else.