标题上的Wordpress省略号

时间:2015-01-28 01:40:58

标签: php wordpress wordpress-plugin ellipsis

我尝试在此帖子上执行相同的操作:Wordpress Titles: If Longer Than 50 Characters, Show Ellipsis

但根本没有运气,你能说出我做错了什么吗?

这是我的代码段

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>";
}

}

1 个答案:

答案 0 :(得分:0)

我自己有以下功能,它完美无缺。

在我的functions.php文件的顶部,我使用以下代码:

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

之后,您可以在任何地方使用它:

<?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)"; ?>