I have written this code to loop through the latest 6 posts on my blog and display them in a special box but there is a slight problem, when i write longer titles the title moves to the next line. I cant increase the width of my title div so I need to write an if condition to show a “…” if the title moves to the next line after lets say 20 characters.
<div id="freshlyWrapper">
<div id="freshlyposts">
<?php
$freshlyIonised = new WP_Query();
$freshlyIonised->query('category_name=FreshlyIonised&showposts=6');
while($freshlyIonised->have_posts()):
$freshlyIonised->the_post();
?>
<div class="freshlyionisedbox"><h3><a style='text-decoration: underline; color: #000;' href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
There is a better way, with CSS:
The advantage of this being the semantic content of your page is preserved, and you don’t have to guess at how many characters will fit on the user’s screen in a certain number of pixels.
The substr way:
This is what I usually use, to prevent words from cutting:
Try substr:
Implement like so:
Try…
you don’t need condition, apply this