SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    reputation consultant ThaVincy's Avatar
    Join Date
    Mar 2003
    Location
    Flanders
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question How to trim the number of returned characters?

    Hi all,

    We are designing a (wordpress based) website for a client, on which we would like to show all available subpages of a certain parent page; which works fine.

    We would like the returned $content to be limited to 70 characters, in stead of the entire text. I understand that PHP uses a "rtrim" function to accomplish this, but I have no idea on how to use this in our code. Is it used in the apply_filters part?

    This is the code we use:

    Code:
    <!-- load practice areas -->
    <div class="three_col_wide">
    <?php
        $mypages = get_pages( array( 'child_of' => $post->ID, 'sort_column' => 'post_date', 'sort_order' => 'asc' ) );
    
        foreach( $mypages as $page ) {        
            $content = $page->post_content;
            if ( ! $content ) // Check for empty page
                continue;
    
            $content = apply_filters( 'the_content', $content );
        ?>
        <div class="column"><h2><?php echo $page->post_title; ?></h2>
        <?php echo $content; ?><p><a href="<?php echo get_page_link( $page->ID ); ?>" class="button"><?php _e('[:nl]meer info[:fr]en lire plus[:en]more info'); ?></a></p></div>
        <?php
        }    
    ?>
    </div>
    Any help would be greatly appreciated.
    ThaVincy | redesigning your image
    Twizted Imagebuilding personal and corporate reputation solutions

  2. #2
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Location
    United Kingdom
    Posts
    78
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    For this I'd suggest using the substr() function. It accepts three arguments, the first being the strings content, the second is the start value (number of characters through the content), and the third (optional) is the number of characters to grab. It can be easily applied like so:

    PHP Code:
    <?php

    $content 
    =  'This is some text.';

    echo 
    substr($content010); #gets the first 10 characters

    #Outputs: This is so
    ?>

  3. #3
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,071
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by modernW View Post
    For this I'd suggest using the substr() function. It accepts three arguments, the first being the strings content, the second is the start value (number of characters through the content), and the third (optional) is the number of characters to grab. It can be easily applied like so:
    Although that will work, keep in mind that WordPress stores HTML tags too. So you may be grabbing the first 10 characters that includes an opening <p> tag and not a closing tag.

    Personally, I think you need to use the Excerpt field in WordPress and pull that field instead of the content, or you need to at least strip all of the tags from the content before grabbing the first 10 characters.

    PHP Code:
    $content strip_tags(apply_filters'the_content'$content ));
    $content substr($content010); #gets the first 10 characters 
    Another technique can use str_word_count
    PHP Code:
    $content strip_tags(apply_filters'the_content'$content ));
    $words str_word_count($content1);
    $wordChunks array_chunk($words50); // chunk the words into groups of 50
    $first50words implode(' '$words[0]); // makes a paragraph of the first 50 words 
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  4. #4
    reputation consultant ThaVincy's Avatar
    Join Date
    Mar 2003
    Location
    Flanders
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you so much for the replies. Works great!
    ThaVincy | redesigning your image
    Twizted Imagebuilding personal and corporate reputation solutions


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •