...

View Full Version : Resolved Limiting Char in title (wordpress widget)



kor81
08-24-2011, 06:57 AM
I Downloaded a wordpress widget and i was wondering if it was possibleto limit the char in the title.

Snippet from the area that gives us the title


if ($author_has_url) echo '</a>';
echo $url;
echo $comment->post_title;

complete code

/*
* Called by register_sidebar_widget() function.
* Rendering of the widget happens here.
*/
function widget($args) {
global $wpdb;

extract($args);

$options = get_option($this->_folder);
$this->validate_options($options);

if (is_single() && $options['show_in_post'] != 'checked') return;
if (is_page() && $options['show_in_page'] != 'checked') return;

$author_emails = array();
if (!empty($options['author_emails'])) {
$author_emails = explode(',', $options['author_emails']);
}

$sql = "SELECT a.*, b.post_title from $wpdb->comments a JOIN $wpdb->posts b ON a.comment_post_id = b.id WHERE comment_approved= '1' AND a.comment_type != 'pingback'
ORDER BY comment_date DESC LIMIT " . $options['num_of_comments'];

$comments = $wpdb->get_results($sql);

echo $before_widget;
echo $before_title;
echo is_single() && ($options['single_mode'] == 'checked') ? $options['single_mode_title'] : $options['title'];
echo $after_title;

echo '<div class="rcg-div">';
if ($comments) {
echo '<ul>';
foreach ($comments as $comment) {
$author_has_url = !(empty($comment->comment_author_url) || 'http://' == $comment->comment_author_url);
$is_author = in_array($comment->comment_author_email, $author_emails);
$url_author = '<a href="' . $comment->comment_author_url . '" title="' . $comment->comment_author . '" rel="external nofollow" target="_blank">';
$url = '<a href="'. get_permalink($comment->comment_post_ID).'#comment-'.$comment->comment_ID .'" title="'.$comment->comment_author .' | '.get_the_title($comment->comment_post_ID).'">';
echo '<li ' . ($is_author ? 'class="rcg-author"' : '') . '><div class="rcg-wrapper">';
echo '<div class="rcg-avatar">';
echo $author_has_url ? $url_author : '<span title="' . $comment->comment_author . '">';
echo get_avatar($comment->comment_author_email, intval($options['gravatar_width']));
echo $author_has_url ? '</a>' : '</span>';
echo '</div>';

echo '<div class="rcg-text" style="padding-left:' . (intval($options['gravatar_width']) + 10) . 'px">';
if ($author_has_url) echo $url_author;

if ($author_has_url) echo '</a>';
echo $url;
echo $comment->post_title;

echo '</a><br/> ';

echo '</div>';
echo '</div></li>';
}
echo '</ul>';
}
if ($options['link_to_us'] == 'checked') {
echo '<div class="rcg-link"><a href="' . $this->_link . '" target="_blank">'. __('Get this widget for your own blog free!', $this->_folder) . '</a></div>';
}
echo '</div>';
echo $after_widget;
}

kunz
08-24-2011, 02:39 PM
You could substr() to select only a certain portion of the title string.

kor81
08-24-2011, 09:08 PM
Ok, i got it thank you kunz

i replaced this

if ($author_has_url) echo '</a>';
echo $url;
echo $comment->post_title;

with this

if (strlen($post->post_title) > 35) '</a>';
echo $url;
echo substr(the_title($before = '', $after = '', FALSE), 0, 35) . '...';


edit, nevermid that didn't work, its pulling the post title for the last post and not the title from the post last commented post

Fou-Lu
08-24-2011, 10:39 PM
That looks mostly right, but the variables have changed. A lot.
Based on your first block of code, you would use:


if ($author_has_url) echo '</a>';
echo $url;
echo strlen($comment->post_title) > 35 ? substr($comment->post_title, 0, 35) . '...' : $comment->post_title;


If that's not what you intend to do, the logic is correct you'd just need to track down which block to modify.

kor81
08-25-2011, 08:28 PM
That looks mostly right, but the variables have changed. A lot.
Based on your first block of code, you would use:


if ($author_has_url) echo '</a>';
echo $url;
echo strlen($comment->post_title) > 35 ? substr($comment->post_title, 0, 35) . '...' : $comment->post_title;


If that's not what you intend to do, the logic is correct you'd just need to track down which block to modify.

That worked, thank you.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum