• Resolved lf161200

    (@lf161200)


    The theme I’m using has a function to display a list of four posts that are the most popular, deemed by the number of comments they have. I’d like to limit the characters in the popular post results. Here’s the code:

    <?php
    	$sql='SELECT post_title, comment_count, guid
    		FROM wp_posts
    		ORDER BY comment_count DESC
    		LIMIT 4;';
    	$results = $wpdb->get_results($sql);
    
    	foreach ($results as $r) {
    		echo '<li><a href="' . $r->guid . '" title="' . $r->post_title . '"> ' . $r->post_title .
    			' (' . $r->comment_count . ')</a></li>';
    	}
    ?>

    Any help is greatly appreciated. Thanks is advance!

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter lf161200

    (@lf161200)

    Hello again.

    Is anyone up for the challenge of helping me? I really respect the knowledge of everyone on the forum and love their willingness to help.

    Thanks!

    Graphic outlining my issue is here:
    http://www3.ouragility.com/wp-content/uploads/2009/03/Untitled-1.png

    function truncate($string, $max = 20, $replacement = '')
    {
        if (strlen($string) <= $max)
        {
            return $string;
        }
        $leave = $max - strlen ($replacement);
        return substr_replace($string, $replacement, $leave);
    }
    
    echo truncate('something to truncate',10,'...');

    Would output…

    somethi...

    Just pass what you want to trim down into the function…

    Pulled directly off the PHP website, bear in mind whatever you add onto the end (3 dots/periods in example) will count toward the character count (10 in example).

    Thread Starter lf161200

    (@lf161200)

    OK. I get the gist of this…but am not completely clear on how/where to add that function to the original code. Any help? Please?

    <?php
    function truncate($string, $max = 20, $replacement = '')
    {
        if (strlen($string) <= $max)
        {
            return $string;
        }
        $leave = $max - strlen ($replacement);
        return substr_replace($string, $replacement, $leave);
    }
    
    	$sql='SELECT post_title, comment_count, guid
    		FROM wp_posts
    		ORDER BY comment_count DESC
    		LIMIT 4;';
    	$results = $wpdb->get_results($sql);
    
    	foreach ($results as $r) {
    		echo '<li><a href="' . $r->guid . '" title="' .$r->post_title . '"> ' .  truncate($r->post_title,20,'..') .
    			' (' . $r->comment_count . ')</a></li>';
    	}
    ?>

    Should do it…

    Thread Starter lf161200

    (@lf161200)

    Perfect. Thank you so much!

    You’re welcome…

    If you want to re-use that function then it may be more ideal to move the function part into your theme functions.php, just this part…

    function truncate($string, $max = 20, $replacement = '')
    {
        if (strlen($string) <= $max)
        {
            return $string;
        }
        $leave = $max - strlen ($replacement);
        return substr_replace($string, $replacement, $leave);
    }
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Limit characters in popular posts results’ is closed to new replies.