WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Add Special CSS Class to Titles with length > x characters? (8 posts)

  1. theiconoclast31
    Member
    Posted 7 years ago #

    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 :P

  2. vkaryl
    Member
    Posted 7 years ago #

    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.

  3. theiconoclast31
    Member
    Posted 7 years ago #

    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.

  4. benzin
    Member
    Posted 7 years ago #

    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..

  5. theiconoclast31
    Member
    Posted 7 years ago #

    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.

  6. theiconoclast31
    Member
    Posted 7 years ago #

    <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.

  7. theiconoclast31
    Member
    Posted 7 years ago #

    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.

  8. theiconoclast31
    Member
    Posted 7 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic