What you want to do can probably be handled using js. Not sure about php, but I have definitely seen a math function in js that will do that sort of thing. You should check around the script sites for something that will do that.
Although I couldn’t find the exact script I wanted, I found something very close for a “Template Engine” called Smarty. Maybe someone who knows more about PHP would spare some time to make a WP plugin using the same technique:
http://www.phpinsider.com/smarty-forum/viewtopic.php?t=533
Nothing urgent, thanks for the response.
EDIT: To clarify, there’s a very popular JS truncating script, but it breaks if there’s any HTML within the text.
Your title in index.php is something like <h3 id=”post-<?php the_ID(); ?>”> change to this in you post title <h3 id=”post-<?php the_ID(); ?>” <?php if (strlen(the_title()) > 50) { echo ” class=\”long-post-title\””; }>
It should work..
It’s not working, even after I change the >
in your code to ?>
.
EDIT: I think there’s something wrong with the conditional, because the class isn’t even showing up in the source.
<h2<?php if (strlen("the_title()") > 50) { echo " class=\"long-post-title\""; }?>>
That’s the code I’m currently using. It doesn’t output anything, but I can’t find any mistakes.
Sorry for the triple post.
<?php if (strlen('wp_title('', false)') > "50") { echo 'class="long-post-title"'; }?>
I’ve been trying to figure this out for the past hour, and the above code is functional, but the wp_title('', false)
isn’t working. If you simply replace that piece with a regular string, it will work. I think I’d have to define a variable called $posttitle, but I have no idea how to define variables.
SUCCESS!
WOW! Thanks a lot, by the way, benzin. I totally forgot to thank you for the response. Here’s the solution.
1. Immediately after the ‘loop’, add: <?php $posttitle = the_title('', '', false); ?>
2. This is my h2 tag: <h2<?php if (strlen($posttitle) > "50") { echo " class="long-post-title""; }?>>
Also, I’m not sure if it works with more than one post, because I don’t know anything about PHP variables. I have my first post running in its own loop.