Support » Plugins » Add Special CSS Class to Titles with length > x characters?

  • Resolved theiconoclast31


    Erm, well, I think the title of the thread basically asked the question xP.

    Instead of truncating the post title, which looks ugly, I’d like to wrap titles with more than x characters to have a special CSS class, let’s call it “long-post-title”. Then, I’d just open up my stylesheet and define it as

    .long-post-title {font-size: small; }

    It doesn’t have to be fancy – I don’t know how to write PHP, but I sure can edit it 😛

Viewing 7 replies - 1 through 7 (of 7 total)
  • 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:

    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.


    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.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Add Special CSS Class to Titles with length > x characters?’ is closed to new replies.