• Hey,

    Got everything set up nicely – popped a widget in the sidebar & it works fine getting the 5 latest posts. Tried to do a shortcode however & it doesn’t work – all it brings back is one link for the front page even though it’s set to just bring back posts only, not pages.

    My code is this:

    <?php echo do_shortcode("[google_top_content pageviews=0 number=5 showhome=no time=2628000 contentfilter=post]"); ?>

    Any ideas?

    Thanks!

    http://ww.wp.xz.cn/plugins/google-analytics-top-posts-widget/

Viewing 8 replies - 16 through 23 (of 23 total)
  • Plugin Author Justin Sternberg

    (@jtsternberg)

    If you’re using the shortcode, add update=true to your list of shortcode parameters to bust the cache. After it’s been refreshed, remove the argument. It’s important you do that so you don’t have to call Google’s API w/ every page-load by your visitors which will slow the page down quite a bit.

    Thanks for responding.

    When adding the update=true parameter like this:

    <?php echo do_shortcode( '[google_top_content pageviews=1 number=1 update=true]' ); ?>

    I still get the previous list of the plugin (and I did reload it a few times and gave it time to clean up), and not a list with only one link.

    Plugin Author Justin Sternberg

    (@jtsternberg)

    If you add the widget to a sidebar w/ all the same parameters, does it pull in the same list?

    If you add the widget to a sidebar w/ all the same parameters, does it pull in the same list?

    My theme has no widget area, and though I do want this plugin to work, I’m not keen on rewriting my theme to check something for a plugin. Can I also debug it in another way?

    Plugin Author Justin Sternberg

    (@jtsternberg)

    Looks like the ‘update’ parameter wasn’t working correctly. I just pushed an update (1.5.3). Please update and try again. Also, you’ll be able to know if the data is cached by viewing the source code code. You’ll see <!-- using transient --> if the data is cached and <!-- not using transient --> if it is not (which should be the case if you set ‘update=true’ in your shortcode parameters).

    Thanks Justin, with 1.5.3. update works like a charm. What a great parameter, much more convenient than searching the database for the transients.

    For me, there’s only small thing left. And that is that catlimit does not work on the parent level (at least, I cannot make it to work).

    For example, with a category tree like:

    Parent (ID 1)
        Child A (ID 2)
        Child B (ID 3)

    catlimit=2 will return the posts in Child A, but catlimit=1 returns nothing, since (strictly speaking) there are no posts in Parent but only in Child A and Child B.

    But this is not convenient when adding future categories, and WordPress itself also looks into the child categories when the parent is specified. For example:

    query_posts(array('category_name' => 'parent'));
    
    if (have_posts()) : while (have_posts()) : the_post();
    ...

    Will give all the posts in Parent, Child A, and Child B.

    -> Can the catlimit parameter of this plugin also take the child categories into consideration?

    Can the catlimit parameter of this plugin also take the child categories into consideration?

    I’ve had a plugin developer look into this, and she implemented this. I’ve tested it on two websites: it works with both the shortcode and the widget. One “limitation” is that only one parent category can be specific as ‘catlimit’ parameter, but that’s how I wanted to use it.

    The following changes were made in google-analytics-top-posts-widget.php:

    Add the following function:

    public function post_is_in_descendant_category( $cats, $_post = null ) {
    		foreach ( (array) $cats as $cat ) {
    			// get_term_children() accepts integer ID only
    			$descendants = get_term_children( (int) $cat, 'category' );
    			if ( $descendants && in_category( $descendants, $_post ) )
    				return true;
    		}
    		return false;
    	}

    Replace:

    if ( $atts['catlimit'] != '' ) {
      $limit_array = array();
      $catlimits = esc_attr( $atts['catlimit'] );
      $catlimits = explode( ', ', $catlimits );
      foreach ( $catlimits as $catlimit ) {
        if ( in_category( $catlimit, $wppost ) ) $limit_array[] = $wppost->ID;
      }
      if ( !in_array( $wppost->ID, $limit_array ) )
        continue;
    
    }

    With:

    if ( $atts['catlimit'] != '' ) {
      $limit_array = array();
      $catlimits = esc_attr( $atts['catlimit'] );
      $catlimits = explode( ', ', $catlimits );
      foreach ( $catlimits as $catlimit ) {
        if ( in_category( $catlimit, $wppost ) || $this->post_is_in_descendant_category( $catlimit, $wppost ) ) $limit_array[] = $wppost->ID;
      }
      if ( !in_array( $wppost->ID, $limit_array ) )
        continue;
    
    }

    Justin, could this code (after your review) perhaps be added to the main plugin?

    It would be very cool if this could be added to the plugin.

    Thanks in advance for considering it,

Viewing 8 replies - 16 through 23 (of 23 total)

The topic ‘Widget works but shortcode doesn't’ is closed to new replies.