WordPress.org

Ready to get started?Download WordPress

Forums

Google Analytics Top Content Widget
[resolved] Show number of views in output (12 posts)

  1. Contemplative Computing
    Member
    Posted 1 year ago #

    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.

    Thanks,
    Matthew

    http://wordpress.org/extend/plugins/google-analytics-top-posts-widget/

  2. Contemplative Computing
    Member
    Posted 1 year ago #

    Actually already answered here:

    http://wordpress.org/support/topic/top-viewed-content-couple-of-tweeks-needed?replies=20#post-3816989

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

  3. Contemplative Computing
    Member
    Posted 1 year ago #

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

  4. Contemplative Computing
    Member
    Posted 1 year ago #

    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.

  5. Justin Sternberg
    Member
    Plugin Author

    Posted 1 year ago #

    Happy to help. :)

  6. Justin Sternberg
    Member
    Plugin Author

    Posted 1 year ago #

    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.

  7. Contemplative Computing
    Member
    Posted 1 year ago #

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

  8. Justin Sternberg
    Member
    Plugin Author

    Posted 1 year ago #

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

  9. Contemplative Computing
    Member
    Posted 1 year ago #

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

  10. Contemplative Computing
    Member
    Posted 1 year ago #

    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?

    Thanks.

  11. Justin Sternberg
    Member
    Plugin Author

    Posted 1 year ago #

    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

  12. Contemplative Computing
    Member
    Posted 1 year ago #

    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!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.