Thanks, I think that’s on the right path. I’m having trouble implementing it though. This is what I tried:
function short_title($after = '', $length) {
$mytitleorig = get_the_title();
$mytitlehtml = htmlentities($mytitleorig);
$mytitle = html_entity_decode($mytitlehtml);
if ( mb_strlen($mytitle ) > $length ) {
$mytitle = mb_substr($mytitle,0,$length);
echo $mytitle . $after;
} else {
echo $mytitle;
}
}
any ideas?
also tried
function short_title() {
$mytitleorig = get_the_title();
$mytitlehtml = htmlentities($mytitleorig);
$title = html_entity_decode($mytitlehtml);
$limit = "49";
$pad="...";
if(strlen($title) <= $limit) {
echo $title;
} else {
$title = substr($title, 0, $limit) . $pad;
echo $title;
}
}
needed a bit of reshuffeling – not easy to see what these html functions are doing.
this should hopefully work:
function short_title() {
$mytitleorig = get_the_title();
$html_dedode_title = html_entity_decode($mytitleorig);
$limit = "49";
$pad="...";
if(strlen($html_dedode_title) >= ($limit+3)) {
$html_dedode_title = substr($html_dedode_title, 0, $limit) . $pad; }
$title = htmlentities($html_dedode_title);
echo $title;
}
basicaly, first decode the html entities, then check the string length, and then recode the character into html entities if appropriate.
i tidyed the ‘if’ statement a bit, added a 3 to the limit so that everything ends at the same point (you can take that out if).
good luck 😉
almost works perfectly.. it solved the problem for the brackets, and all lines are now equal length, but the apostrophes are still giving me trouble. they now show up right in the browser as ’ or ' instead of ‘
sorry they show up as
'
and
’
right in the browser.
ahhh, i thought if i put them in as code form they wouldn’t convert.
&# 039 ; and
&# 8217 ;
if you look into the documentation of the html-entity functions, there are some parameter to do with quotes, and also the character set – maybe one of them is responsible (?)
got it. used
function short_title() {
$mytitleorig = get_the_title();
$html_decode_title = html_entity_decode($mytitleorig, ENT_QUOTES, "UTF-8");
$limit = "49";
$pad="...";
if(strlen($html_decode_title) >= ($limit+3)) {
$html_decode_title = substr($html_decode_title, 0, $limit) . $pad; }
$title = utf8_encode(htmlentities($html_decode_title));
echo $title;
}
thanks for the help man. 🙂
actually, that code still gave me some trouble, turning some of the quotes into a’s with ^’s over them. turns out the htmlentities() function is unneccessary.
this code seems to work perfectly, for anyone else wanting to do this.
function short_title() {
$mytitleorig = get_the_title();
$title = html_entity_decode($mytitleorig, ENT_QUOTES, "UTF-8");
$limit = "49";
$pad="...";
if(strlen($title) >= ($limit+3)) {
$title = substr($title, 0, $limit) . $pad; }
echo $title;
}
There’s a plugin call truncate-title. Add the function to theme’s functions.php without the filter. Then add filter before your recent post, then remove filter after recent post.
This way u wont need to _decode_ _htmlentities_ the title yourself. WP will do for.
Yours also can work if use filter.
This also works (I use it all the time). Just change “10” to meet your needs.
// Post Title Limit
function short_title() {
$limit = 10+1;
$title = explode(' ', get_the_title(), $limit);
array_pop($title);
$title = implode(" ",$title).$ending;
echo $title;
}
Excerpt’s too:
// Excerpt Word Limit
function excerpt($teaser_word_count) {
$limit2 = 15+1;
$excerpt = explode(' ', get_the_excerpt(), $limit2);
array_pop($excerpt);
$excerpt = implode(" ",$excerpt).$ending;
echo $excerpt;
}
After declaring these functions, I use them in sub-loops:
<div class="story local_news">
<?php $sub_loop_2 = new WP_Query("cat=3&showposts=1"); while ($sub_loop_2->have_posts()) : $sub_loop_2->the_post(); ?>
<div class="imgcont left">
<div class="thumbnail"><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( array(187,105) ); ?></a></div>
</div>
<div class="txtcont right">
<h2><a href="<?php the_permalink(); ?>"><?php short_title(); ?></a></h2>
<p><?php excerpt($teaser_word_count) ?> [...]</p>
<p class="permalink"><a href="<?php the_permalink(); ?>">READ MORE <img src="<?php bloginfo('template_url'); ?>/custom/images/arrow.png" /></a></p>
</div>
<?php endwhile; ?>
</div>
Moderator
t-p
(@t-p)
hi theinfinityes,
I tried your code – placed it in functions.php- but get the following error for line $title = html_entity_decode($mytitleorig, ENT_QUOTES, “UTF-8”);
Warning: cannot yet handle MBCS in html_entity_decode()!
could you please explain step-by-step how to implement it. I will really appreciate it. thanks.