PHP 通过始终位于页面顶部的简码

PHP via shortcode always on top of page

我有一个 wordpress 插件可以显示最受欢迎的帖子...

但是当我通过短代码添加它时,它总是到达页面顶部而不是我放置它的位置...我将插件 PHPs 中的每个 echo 更改为 return 但它没有帮助... 这是我在 functions.php:

中的简码

function top_news(){
  $args = array(
    'limit' => 15,
    'range' => 'daily',
'freshness' => 1,
 'order_by' => 'views',
'post_type' => 'post',
'stats_views' => 1,
'stats_author' => 1,
 'stats_date' => 1,
'wpp_start' => '<table class="topnachrichten"><tr><th>Datum</th><th>Top Nachrichten von Heute</th><th>Leser</th></tr>',
    'wpp_end' => '</table>',
'stats_date_format' => 'd',
'excerpt_by_words' => 1,
    'excerpt_length' => 35,
'title_length' => 66,
 'post_html' => '<tr><td class="datum">{date}. Aug</td><td class="stext"><details>
  <summary><a href="{url}">{title}</a><span class="plus">+</span></summary>{summary}<br><a href="{url}">Weiterlesen</a></details></td><td class="views">{views}</td></tr>'


);
 wpp_get_mostpopular( $args );
 return $args;
}
add_shortcode( 'topnews', 'top_news' );

你知道我能做什么吗?

谢谢, 直到

阅读 wpp_get_mostpopular 的文档,它指出该函数实际上打印了流行的 posts。这意味着您的流行 posts 在它 returns 之前打印出来,并且由于所有短代码都在 posts 内容被打印之前处理,这就是为什么您的流行 posts 总是在 post 内容之前(顶部)打印。

因此,您可以做的是在缓冲区中捕获所有流行的 post。

function top_news(){
     $args = array (
        'limit' => 15,
        'range' => 'daily',
        'freshness' => 1,
        'order_by' => 'views',
        'post_type' => 'post',
        'stats_views' => 1,
        'stats_author' => 1,
        'stats_date' => 1,
        'wpp_start' => '<table class="topnachrichten"><tr><th>Datum</th><th>Top Nachrichten von Heute</th><th>Leser</th></tr>',
        'wpp_end' => '</table>',
        'stats_date_format' => 'd',
        'excerpt_by_words' => 1,
        'excerpt_length' => 35,
        'title_length' => 66,
        'post_html' => '<tr><td class="datum">{date}. Aug</td><td class="stext"><details>
        <summary><a href="{url}">{title}</a><span class="plus">+</span></summary>{summary}<br><a href="{url}">Weiterlesen</a></details></td><td class="views">{views}</td></tr>'
    );

    ob_start();
    wpp_get_mostpopular( $args );
    $output = ob_get_contents();
    ob_end_clean();

    return $output;
}
add_shortcode( 'topnews', 'top_news' );