WordPress.org

Ready to get started?Download WordPress

Forums

How to use strlen with the_title()? (9 posts)

  1. baal666
    Member
    Posted 4 years ago #

    Hi,

    I'm trying to use strlen to get the number of characters in the title of the post.

    What is wrong with this code?

    <?php
    
    $sss = the_title();
    echo strlen($sss);
    ?>

    Thanks!

  2. baal666
    Member
    Posted 4 years ago #

    What I am trying to do is this:

    I have a 300px box and when the title is short or long (with the excerpt) it changes how it look... I'd like to set something so that when the title is short, it would add a <p> or
    automatically.

    Is it possible?

  3. alchymyth
    The Sweeper & Moderator
    Posted 4 years ago #

    to get the title as a string, use

    get_the_title()
    http://codex.wordpress.org/Template_Tags/get_the_title

    or

    the_title('','',false)
    http://codex.wordpress.org/Template_Tags/the_title

  4. baal666
    Member
    Posted 4 years ago #

    Thanks a lot!

    Next step for me is to find a way to make it work correctly and put a if command so that it will add a <p> or
    when it is shorter...

    I'll try to do it by myself... and I will ask if I can't... Thanks

  5. baal666
    Member
    Posted 4 years ago #

    Damn it I really am bad at PHP... What is wrong with my code?

    <?php
    
    $sss = the_title('','',false);
    if (strlen($sss)<43)
    {echo '<p/>';
    ?>
  6. baal666
    Member
    Posted 4 years ago #

    Tried this as well... a failure

    <?php
    
    $sss = the_title('','',false);
    $small= the_title('','<p/>');
    $big = the_title();
    if (strlen($sss)<43) return $small;
    else return $big;
    
    ?>
  7. baal666
    Member
    Posted 4 years ago #

    Anybody can help? Thanks

  8. baal666
    Member
    Posted 4 years ago #

    Now it seems to work... `<?php
    $sss = the_title('','',false);
    if (strlen($sss)<43) the_title('','<p/>');
    else the_title();
    ?>
    `

    Thanks anyway

  9. alchymyth
    The Sweeper & Moderator
    Posted 4 years ago #

    your code could look like this:

    <?php
    
    $sss = the_title('','',false);
    $small = $sss . '<br />';
    $big = $sss;
    if (strlen($sss)<43) echo $small;
    else echo $big;
    
    ?>

    this $small= $sss . '<br />'; should add a linebreak to the string that contains the short title;
    the '<p/>' is not a valid html or other code.

    concatenate strings:
    http://php.net/manual/en/language.operators.string.php

    return will not print anything, it is for use with functions:
    http://php.net/manual/en/function.return.php

    use echo to output things to the screen:
    http://php.net/manual/en/function.echo.php

    edit: just saw your last post;

    well done, your code should work.
    it is actually making good use of the wordpress template tag 'the_title()'

    you could just replace <p/> with <br /> to make it more valid html.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.