0

I tried doing the same procedure on this post :Wordpress Titles: If Longer Than 50 Characters, Show Ellipsis

but no luck at all , can you tell what did i do wrong?

here's my snippet

function wpfp_list_most_favorited($limit=5) {
global $wpdb;
$query = "SELECT post_id, meta_value, post_status FROM $wpdb->postmeta";
$query .= " LEFT JOIN $wpdb->posts ON post_id=$wpdb->posts.ID";
$query .= " WHERE post_status='publish' AND meta_key='".WPFP_META_KEY."' AND meta_value > 0 ORDER BY ROUND(meta_value) DESC LIMIT 0, $limit";
$results = $wpdb->get_results($query);
if ($results) {
    echo "<ul>";
    foreach ($results as $o):
        $p = get_post($o->post_id);
        $post = mb_strimwidth($p, 0, 20, '...');
        echo "<li>";
        echo "<a href='".get_permalink($o->post_id)."' title='". $p->post_title ."'>" . $post->post_id . "</a> ($o->meta_value)";
        echo "</li>";
    endforeach;
    echo "</ul>";
}

}

Community
  • 1
  • 1

1 Answers1

0

I've the following function for myself it works perfectly.

At the top of my functions.php file I use the following code:

function truncate($text, $chars = 25) {
   $text = $text." ";
   $text = substr($text,0,$chars);
   $text = substr($text,0,strrpos($text,' '));
   $text = $text."...";
   return $text;
}

After that you can use it anywhere you want like:

<?php echo truncate($post->post_title, 50); ?>
OR 
<?php echo "<a href='".get_permalink($o->post_id)."' title='". $p->post_title ."'>" . truncate($post->post_title,50) . "</a> ($o->meta_value)"; ?>
Paul
  • 633
  • 2
  • 13
  • 22