dongmo2324 2017-09-20 09:29
浏览 66
已采纳

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

I got a plugin for wordpress to show the most popular posts...

But when I add it via shortcode it always gets to the top of the page and not where I placed it... I changed every echo in the plugin PHPs to return but it didn't helped... Here is my shortcode in 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' );

Do you know what I can do?

Thanks, Till

</div>
  • 写回答

1条回答 默认 最新

  • dongling3243 2017-09-20 09:38
    关注

    Reading documentation of wpp_get_mostpopular, it states that the function actually prints the popular posts. Which means your popular posts are printed out before it returns anything and since all shortcodes are processed before the posts content is being printed that's why your popular posts always prints before (at top) of the post content.

    So, what you can do is catch all the popular posts in a buffer.

    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' );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?