• Resolved marky_uk

    (@marky_uk)


    Hiya

    This may be of use to you. I wanted to show all testimonials but just for one category. Bit of a hack (I’m sure Travis will do something way more elegant) but you can do this by –

    1) Add a shortcode in your page/post of =

    [testimonial id='cat=Company testimonial']

    … where Company testimonial is the testimonial category name.

    2) In the tb-testimonials.php file, go to the shortcode() function and add another IF (in my version here it was added at line 424)

    elseif($id<>'') # category based - added by Mark
      {
      $cat = split("cat=",$id);
      $q = new WP_Query( array( 'post_type'=>'testimonial','post_status'=>'publish','showposts'=>'-1','order_by'=>'menu_order','testimonial_category'=>$cat[1]));
    
      if( $q->have_posts() )
        {
        $return = '<div id="tbtestimonial-listing">';
        while( $q->have_posts() )
          {
          $q->the_post();
          $return .= $this->prepare_testimonial( 'shortcode-all' );
          }
          return $return . '</div>';
        }
        else
          return;
        }

    And that’s it!

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Travis Ballard

    (@ansimation)

    hey Marky, thanks for the snippet. Was this a requested feature? It should be built into the shortcode to show testimonials in a single category. If it isn’t, it will be when I get around to adding in some more nifty stuff for you guys.

    Thanks,
    Travis Ballard

    Thread Starter marky_uk

    (@marky_uk)

    Hi Travis ??

    I’m not sure – I hadn’t requested it but suddenly needed it a few days ago on a site, hehe. It’s a requested one now – i.e. I’d like it in the standard codebase and probably written a bit nicer than my mash (I wanted it to use cat= as a paramater, rather than id= but couldn’t work that out!

    Hope you got my donation too Travis (sent last week) ??

    Mark

    Plugin Author Travis Ballard

    (@ansimation)

    I can definitely get that added in for you. I did get your donation, thank you so much! It was an unexpected surprise and it definitely went to good use. Thank you again!

    Thread Starter marky_uk

    (@marky_uk)

    Wicked, thanks, as always, Travis.

    And glad you got the donation .. was worried it had gone to the wrong person ??

    Be safe,

    Mark

    ccgjosh

    (@ccgjosh)

    I know this post is a little old but I thought I would post an updated version of Mark’s code, getting rid of the depreciated split function and adding a limit.
    NOTE: Remove the space between # 038; in my code — wordpress formats it otherwise.

    elseif($id<>'')
              {
              $defaults = array(
                'cat'=>'',
                'limit'=>'-1'
              );
              $args = wp_parse_args( str_replace('# 038;','&',$id), $defaults );
              $q = new WP_Query( array( 'post_type'=>'testimonial','post_status'=>'publish','showposts'=>$args['limit'],'order_by'=>'menu_order','testimonial_category'=>$args['cat']));
    
              if( $q->have_posts() )
                {
                $return = '<div id="tbtestimonial-listing">';
                while( $q->have_posts() )
                  {
                  $q->the_post();
                  $return .= $this->prepare_testimonial( 'shortcode-all' );
                  }
                  return $return . '</div>';
                }
                else
                  return;
                }

    This should be added after line 440 of tb-testimonials.php, version 1.5.7
    Example of usage: [testimonial id=’cat=Company&limit=4′]

    I just needed the same functionality – This should really be added to the next release of TB Testimonials!

    Anyway, thank you ccgjosh. I tried your code and it seems to work fine. However, I also tried modifying it a little so that the order can be set as well:

    $defaults = array(
    	'cat'=>'',
    	'limit'=>'-1',
    	'order'=>'rand'
    );
    $args = wp_parse_args( str_replace('#038;','&',$id), $defaults );
    $q = new WP_Query( array( 'post_type'=>'testimonial','post_status'=>'publish','showposts'=>$args['limit'],'order_by'=>$args['order'],'testimonial_category'=>$args['cat']));

    Unfortunately the order of the testimonials doesn’t change. Any ideas how to fix this?

    OK further investigated in this issue and found the solution… Sometimes it’s so obvious that you simply don’t see it!
    In the code it says “order_by” which is wrong. It should be “orderby“. By the way: This is also wrong in line 367 (displaying all testimonials); Also line 385 has “‘showposts’ => ‘-1’” and after it “‘showposts’ => 1“, I guess the first one can be removed?!

    Anyway, here is the correct code which will enable you to not only choose a category and the limit but also the order direction:

    $defaults = array(
    	'cat'=>'',
    	'limit'=>'-1',
    	'order'=>'rand'
    );
    $args = wp_parse_args( str_replace('#038;','&',$id), $defaults );
    
    $q = new WP_Query( array( 'post_type'=>'testimonial','post_status'=>'publish','showposts'=>$args['limit'],'orderby'=>$args['order'],'testimonial_category'=>$args['cat']));

    Just use the code of ccgjosh and replace line 3-8 with the above one.
    Example of usage: [testimonial id=’cat=Company&limit=4&order=rand’]
    (more ordering options: here)

    Is there a plan to add this to the plugin? I need four separate testimonial pages one for each category. I’m using version 1.5.9 I’ve tried the code snippets above and I get a blank page.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘[Plugin: TBTestimonials] Show all testimonials for just ONE category’ is closed to new replies.