1
nick89
Truncating text of users news posts
  • 2004/7/18 0:49

  • nick89

  • Just popping in

  • Posts: 47

  • Since: 2004/7/9 8


On the top pages of the news module I want to trucate the contents of each news item to 30 chars so that big articles can be read in full by clicking it's link. Has anyone ever done this before?

2
Stewdio
Re: Truncating text of users news posts
  • 2004/7/18 0:53

  • Stewdio

  • Community Support Member

  • Posts: 1560

  • Since: 2003/5/7 1


The only way to do this currently is to place whatever text you want in the top section, and then the extra text in the 'extended' text area in the news admin pages.

The new news module I think (can't remember off hand) will give more control over what is displayed and by how much, but don't quote me on that because my brain is a bit numb right now

3
nick89
Re: Truncating text of users news posts
  • 2004/7/18 0:57

  • nick89

  • Just popping in

  • Posts: 47

  • Since: 2004/7/9 8


could i not change the template news_item.html and replace
<p class="itemText"><{$story.text}></p>
WITH
<p class="itemText"><{$story.text|truncate:30:"...":false}></p>

4
nick89
Re: Truncating text of users news posts
  • 2004/7/18 1:09

  • nick89

  • Just popping in

  • Posts: 47

  • Since: 2004/7/9 8


err.. wait that would truncate the article too....
hmm... what if i made a new tempplate file news_item_condensed.html with the above and chane news_index.html to include this file...

5
tl
Re: Truncating text of users news posts
  • 2004/7/18 1:34

  • tl

  • Friend of XOOPS

  • Posts: 999

  • Since: 2002/6/23


Since both index.php and article.php use the same template, so it would definitely a way to go. If you do that, don't forget to add the calling of the new template file in xoops_version.php file.

6
nick89
Re: Truncating text of users news posts
  • 2004/7/18 1:37

  • nick89

  • Just popping in

  • Posts: 47

  • Since: 2004/7/9 8


After a bit more research i have found the best way. Editting submit.php:

Here is the store process:
$story = new NewsStory();
    
$story->setTitle($subject);
    
$story->setHometext($message);
    
$story->setUid($uid);
    
$story->setTopicId($topic_id);
    
$story->setHostname(xoops_getenv('REMOTE_ADDR'));
    
$story->setNohtml($nohtml_db);
    
$nosmiley = isset($nosmiley) ? intval($nosmiley) : 0;
    
$notifypub = isset($notifypub) ? intval($notifypub) : 0;
    
$story->setNosmiley($nosmiley);
    
$story->setNotifyPub($notifypub);
    
$story->setType('user');
    if ( 
$xoopsModuleConfig['autoapprove'] == ) {
        
$approve 1;
        
$story->setApproved($approve);
        
$story->setPublished(time());
        
$story->setExpired(0);
        
$story->setTopicalign('R');
    }
    
$result $story->store();


Now the bit $story->setHometext($message);
can be changed to
$story->setBodytext($message);
then you can add
$story->setHometext(substr($message,0,30).'...');
(i haven't completely worked this out, will substr complain if the string is less than 30 chars? and this will still add the ... to messages less than 30 chars.

7
nick89
Re: Truncating text of users news posts
  • 2004/7/18 1:41

  • nick89

  • Just popping in

  • Posts: 47

  • Since: 2004/7/9 8


my php knowledge isn't very good - maybee someone would be nice enough to rewrite the $story->setHometext(substr($message,0,30).'...');

so that it checks it is longer than 30 chars before truncating it?

8
nick89
Re: Truncating text of users news posts
  • 2004/7/18 1:48

  • nick89

  • Just popping in

  • Posts: 47

  • Since: 2004/7/9 8


here is my attempt
if(strlen($message) < 30){
$story->setHometext($message);
}else{
$story->setHometext(substr($message,0,30).'...');
}

will this work?

9
nick89
Re: Truncating text of users news posts
  • 2004/7/18 1:52

  • nick89

  • Just popping in

  • Posts: 47

  • Since: 2004/7/9 8


how does XOOPS handle formatting - would I have to do some sort of strip tags before substring?

10
Dave_L
Re: Truncating text of users news posts
  • 2004/7/18 2:33

  • Dave_L

  • XOOPS is my life!

  • Posts: 2277

  • Since: 2003/11/7


You could also use xoops_substr(). It does basically the same thing as your if-else:

$story->setHometext(xoops_substr($message, 0, 30));

Login

Who's Online

168 user(s) are online (103 user(s) are browsing Support Forums)


Members: 0


Guests: 168


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: May 31
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits