我有一个 wordpress 插件可以显示最受欢迎的帖子...
但是当我通过短代码添加它时,它总是到达页面顶部而不是我放置它的位置...我更改了插件 PHP 中的每个 echo 以返回但它没有帮助...这是我的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
的文档,它指出该函数实际上打印了热门帖子。这意味着您的热门帖子会在返回任何内容之前打印出来,并且由于所有短代码都在打印帖子内容之前进行处理,这就是为什么您的热门帖子总是在帖子内容之前(在顶部)打印的原因。
因此,您可以做的是在缓冲区中捕获所有热门帖子。
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' );
关于PHP 通过短代码始终位于页面顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46318278/