P
P
Pavel2020-05-21 13:31:53
PHP
Pavel, 2020-05-21 13:31:53

How to make Wordpress shortcode not ignore insertion point?

I'm trying to insert this shortcode into a post:

add_shortcode( 'top10' , 'top10' );
function top10(){
global $wpdb;
$world_table = $wpdb->get_results( "SELECT * FROM subjects WHERE isCountry = 1 ORDER BY conf DESC LIMIT 0,10" );
foreach ( $world_table as $item ) {      
echo " $item->subject - $item-> conf ";     
}}


When pasted, it displays the data at the top of the page, not reacting to the insertion point [top10]. As I understand it, you need to use not echo, but return, but in the return loop it displays only the first value.

Can you please tell me how to pull the return out of the loop and keep the shortcode working?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Vorotnev, 2020-05-21
@shortcode

$content = '';

foreach ( $world_table as $item ) {      
    $content .= " {$item->subject} - {$item->conf} ";     
}

return $content;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question