PHP 通过短代码始终位于页面顶部

标签 php wordpress shortcode

我有一个 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/

相关文章:

php - 如何使用 php 将 html 标记写入另一个 html 文件?

mysql - 无法在我的 Wordpress 数据库上运行简单的 SQL 查询(将网站下载到本地主机)

wordpress - 如何在 WordPress 中仅关闭 *标签* 的永久链接?

php - 插件内的 Wordpress 简码

php - PHP 中短代码的正则表达式模式

javascript - php 库无法与 AJAX 配合使用的结果

php - 将日期导入 MySQL

php - 使用索引 INT 或索引 TIMESTAMP 更快地在大表中进行 MySQL SELECT

php - PDO 在 longtext 列上返回 NULL,即使它不是 NULL

php - get_the_content 无法在短代码中工作