How to restrict RSS titles in WP sidebar to one line

Discussion in 'PHP' started by GPS Mapper, Sep 26, 2008.

  1. #1
    I'm placing an RSS feed (Amazon bestsellers) in my WordPress sidebar, but the titles are up to five lines long! Is there any way to restrict these to a single line? I'm a real newbie at PHP and any help would be greatly appreciated.

    Here's the code I'm using:
    
    <?php // Get RSS Feed(s)
    include_once(ABSPATH . WPINC . '/rss.php');
    $rss = fetch_rss('http://www.amazon.com/rss/bestsellers/electronics/172526/ref=pd_ts_rss_link?tag=affiliatecode');
    $maxitems = 10;
    $items = array_slice($rss->items, 0, $maxitems);
    ?>
    
    <ul>
    <?php if (empty($items)) echo '<li>No items</li>';
    else
    foreach ( $items as $item ) : ?>
    <li style="margin:0px; padding:0;"><a href='<?php echo $item['link']; ?>' 
    title='<?php echo $item['title']; ?>'>
    <?php echo $item['title']; ?>
    </a></li>
    <?php endforeach; ?>
    </ul>
    Code (markup):
    And here's the result for the first item!:

    #1: Magellan Maestro 4250
    4.3-Inch Widescreen
    Bluetooth Portable GPS
    Navigator (Factory
    Refurbished)
     
    GPS Mapper, Sep 26, 2008 IP
  2. GPS Mapper

    GPS Mapper Peon

    Messages:
    267
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    An alternative I would be very happy with is being able to restrict the display to a certain number of characters. Anyone?
     
    GPS Mapper, Sep 27, 2008 IP
  3. GPS Mapper

    GPS Mapper Peon

    Messages:
    267
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Okay, I found this code on another forum to restrict the display to 50 characters:

    echo ( strlen($str)<55 ? $str : substr($str, 0, 50).'...' );
    Code (markup):
    But where do I put it? And do I need to wrap it like this...?

    <?php echo ( strlen($str)<55 ? $str : substr($str, 0, 50).'...' ); ?>
    Code (markup):
    I'm really new at this! :eek:
     
    GPS Mapper, Sep 27, 2008 IP
  4. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #4
    You already get it =)
    Most probably it will be here.
    <?php echo ( strlen($item['title'])<55 ? $item['title'] : substr($item['title'], 0, 50).'...' ); ?>
    </a></li>
    <?php endforeach; ?>
    </ul>
    PHP:
     
    ads2help, Sep 27, 2008 IP
  5. GPS Mapper

    GPS Mapper Peon

    Messages:
    267
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Oh, I'm so close I can taste it!

    Do I need to strip out any of the preceding lines of code? It didn't work to just drop that line in.

    Thanks for taking the time to help!
     
    GPS Mapper, Sep 27, 2008 IP
  6. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #6
    What i mean was this. Replace the whole thing you posted with this:

    <?php // Get RSS Feed(s)
    include_once(ABSPATH . WPINC . '/rss.php');
    $rss = fetch_rss('http://www.amazon.com/rss/bestsellers/electronics/172526/ref=pd_ts_rss_link?tag=affiliatecode');
    $maxitems = 10;
    $items = array_slice($rss->items, 0, $maxitems);
    ?>
    
    <ul>
    <?php if (empty($items)) echo '<li>No items</li>';
    else
    foreach ( $items as $item ) : ?>
    <li style="margin:0px; padding:0;"><a href='<?php echo $item['link']; ?>' 
    title='<?php echo $item['title']; ?>'>
    <?php echo ( strlen($item['title'])<55 ? $item['title'] : substr($item['title'], 0, 50).'...' ); ?>
    </a></li>
    <?php endforeach; ?>
    </ul>
    PHP:
     
    ads2help, Sep 27, 2008 IP
    GPS Mapper likes this.
  7. GPS Mapper

    GPS Mapper Peon

    Messages:
    267
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Excellent! That did it. Looks like I'll need to go closer to 25 characters to keep it on one line in the sidebar.

    Hey, it turns out that Amazon uses excess spaces or a return or something on some product titles, but I'm only seeing it on two out of ten in the list.

    Thank you so much! I gave a reputation bump for you.
     
    GPS Mapper, Sep 27, 2008 IP
  8. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #8
    thanks. whitespaces? use trim() to get rid of extra spaces

    replace the line
    <?php echo ( strlen($item['title'])<55 ? $item['title'] : substr($item['title'], 0, 50).'...' ); ?>
    PHP:
    by this
    <?php echo ( strlen($item['title'])<55 ? trim($item['title']) : substr(trim($item['title']), 0, 50).'...' ); ?>
    PHP:
     
    ads2help, Sep 27, 2008 IP
  9. GPS Mapper

    GPS Mapper Peon

    Messages:
    267
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Hmm, I must be wrong about returns, whitespaces, etc. That didn't change anything on the display. The text appears to be pulled from their Title tags and I can see nothing amiss there.
     
    GPS Mapper, Sep 27, 2008 IP
  10. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #10
    I don't get you. Give me the link or a picture?
     
    ads2help, Sep 27, 2008 IP
  11. GPS Mapper

    GPS Mapper Peon

    Messages:
    267
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Sorry I never responded. You can see the list here at the top of the left sidebar. There are some items with white space issues, but I'm actually trying to tackle another problem today.

    Is there any way to strip off the first four characters of each item from the feed? Amazon embeds the ranking in the feed, and I'd like to strip it off so I can go to an ol display rather than an li display. That way I can style the rank number to a different color and make it stand out.
     
    GPS Mapper, Oct 6, 2008 IP
  12. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #12
    i cant see any white space.

    to strip off the first four characters, use substr also

    <?php echo ( strlen($item['title'])<55 ? trim($item['title']) : substr(trim($item['title']), 4, 50).'...' ); ?>
    PHP:
    only difference is the 0 changed to 4 so that it start after 4th character
     
    ads2help, Oct 7, 2008 IP
  13. GPS Mapper

    GPS Mapper Peon

    Messages:
    267
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Hey, I'm beginning to really like PHP! That worked very well. There are still some anomalies showing up in Amazon's feed, but its looking much cleaner.

    Thank you so much for all your help.
     
    GPS Mapper, Oct 7, 2008 IP