We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36763
    • 70 Posts
    Hi guys and gals,
    I've been struggling with this problem and am not having much luck, and not even sure if it can be done.

    What I want to do is show the first 200 characters of a page, but if it has more than 200, then it should also show a "Read more" link, to use in a news listing type scenario.

    This is what I preposed, but am stuck.

    [[+content:ellipsis=`200`:gt=`200`:then=`read more code']]
    


    I thought this kind of thing may work, but I'm a little stuck. Merging to Revo slightly feels like I have to relearn a few things. smiley

    I'm currently using these calls in my template.

    template name: jjnewsitems

    <div class="newsitems">  
    [[+content:ellipsis=`200`]]
    </div>
    



    In the main template:

    <div class="jjnewsgroup">
    [[getResources? &parents=`33` &tpl=`jjnewsitems` &includeContent=`1`]]                                
    </div>


    any help would be great. Thanks, Steve [ed. note: vrdriver last edited this post 12 years, 7 months ago.]
      • 33968
      • 863 Posts
      I think you'll need a custom modifier. Save the following as a snippet called truncate or similar with the following code (not tested!):
      <?php
      $options = explode('|',$options);
      $limit  = intval($options[0]) ? intval($options[0]) : 200;
      $ending = $options[1] ? $options[1] : '…';
      
      if (strlen($input) > $limit) {
          return substr($input,0,$limit).$ending;
      } else return $input;
      


      Then you can do this:
      [[+content:truncate=`200|read more code`]]
      

      You can specify 2 options, separated by a pipe '|'. First is length to limit the text snippet to; 2nd is the ending you would like appended if more than the limit.

      Note: replace that weird character on the second line of the snippet with &#8230;. Forums are still a bit buggy smiley [ed. note: okyanet last edited this post 12 years, 7 months ago.]
        • 36763
        • 70 Posts
        Ah, yes. I that makes sense. Thanks for your help. I'm absolutely stuffed right now, but will try that out tomorrow. Appreciate the quick response, and will let you know how I go. shocked)
          • 36763
          • 70 Posts
          Lucas, thank you so much! That worked beautifully.

          Just for everyone else wanting to do this, I amended my "view more code", so that the whole chunk is like this now.

          <div class="newsitems"> 
          [[+content:truncate=`200`:...<a href="[[~[[+id]]]]">Read more</a>`]]
          </div>