I
I
IQprint2020-05-03 16:41:17
WordPress
IQprint, 2020-05-03 16:41:17

How to make the shortcode information appear where the shortcode itself is inserted?

To display the content of one page on others, we use the following shortcode:

function get_post_page_content( $atts ) {
extract( shortcode_atts( array(
'id' => null,
), $atts ) );

$the_query = new WP_Query( 'page_id='.$id );
while ( $the_query->have_posts() ) {
$the_query->the_post();
the_content();
}
wp_reset_postdata();
}
add_shortcode( 'content_insert', 'get_post_page_content' );

But for some reason it displays the content at the top of the page, and not where the shortcode was inserted. For example, we insert a shortcode after a text paragraph, and the content of the shortcode is displayed first, and then the text paragraph. How to fix it to be inserted where the shortcode is placed?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Orkhan Hasanli, 2020-05-03
@azerphoenix

That's right...

$the_query = new WP_Query( 'page_id='.$id );
while ( $the_query->have_posts() ) {
$the_query->the_post();
the_content();
}
wp_reset_postdata();

here you insert content, but you need to use return to display the content of the shortcode only in the specified place.
Here is a simple example - https://wordpress.stackexchange.com/questions/7555...
I have had this problem before: shortcodes shouldn't display any content (using print or echo), instead return the content to be outputted.
If it's too much trouble converting all of your output statements, or you need to use a function that will always display the output, you can use output buffering. A buffer will 'catch' any echo'd or print'd content and allow you to write it to a variable.

I
IQprint, 2020-05-03
@IQprint

If it's not very difficult, add the correct code using return, we didn't succeed. We will be immensely grateful.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question