Support » Plugins and Hacks » Google Analytics Top Content Widget » [Resolved] Show number of views in output

[Resolved] Show number of views in output

  • Going to start a new thread for this. Thank you for all your work on this plugin so far.

    I need to display the total pageviews for the selected period next to the link to the post. I’m digging into the code for your plugin and trying to see how to extract this value — I think from the complex_report_query() function. I’m pretty weak with PHP so I’d appreciate some guidance. Otherwise, I’ll report back here once I figure it out.



Viewing 11 replies - 1 through 11 (of 11 total)
  • Actually already answered here:


    Apologies for the redundant thread, though this may be helpful for others coming here looking specifically for this answer.

    I would like to be able to style the view counter separately, which would require a separate html element from the title.

    My solution:

    Just below line 318
    $pageviews = $page['children']['children']['ga:pageviews'];

    Then edit line 389

    $list .= '<li><a href="' . $url . '">' . $title . '</a> <span class="gtc_pageviews">['.$pageviews.' Views]</span></li>';

    Add css for .gtc_pageviews at your discretion.

    Easy, thanks to the author’s help finding the value in the nested arrays.

    Plugin Author Justin Sternberg


    Happy to help. 🙂

    Plugin Author Justin Sternberg


    You should not be editing the plugin file itself as it will get overridden with the next update. As stated in the thread that you linked to, you can add the filter in your theme’s functions.php file to accomplish the same thing.

    I tried that and the <span> tag wouldn’t go through using the filter. Any suggestions?

    Plugin Author Justin Sternberg


    Ah, good point. Just pushed up a new version to wp.org. Should be available in your dashboard in the next day.

    Great, thanks Justin! I am now officially a huge fan of your plugin. Maybe I’ll blog about it. 🙂

    It looks like you changed the code for these lines, adding an apply_filters() to the
    list item output, and now I cannot seem to get this hack/filter working again. Would you please let me know how to display the views of the post using the newer version of the plugin?


    Plugin Author Justin Sternberg


    This is still working for me.

    add_filter( 'gtc_pages_filter', 'gtc_add_viewcount_title' );
    function gtc_add_viewcount_title( $pages ) {
      if ( !$pages )
        return false;
      // loop through the pages
      foreach ( $pages as $key => $page ) {
        // and add the page count to the title value
        $pages[$key]['children']['value'] = $pages[$key]['children']['value'] . ' <span style="color:red;" class="gtc_pageviews">['. $pages[$key]['children']['children']['ga:pageviews'] .' Views]</span>';
      return $pages;

    Gives me an output like: http://b.ustin.co/GDnd

    Okay something was weird in my installation where changes were not going through right away. I think it had to do with the plugin organizer plugin. All good now!

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘[Resolved] Show number of views in output’ is closed to new replies.