Support » Fixing WordPress » Cut off long post_title

  • Resolved ReallyConfused

    (@reallyconfused)


    Hi all,

    Firstly, apologies as I know this has been asked before but I can’t quite get any of the suggested ideas to work, so I’m hoping somebody will be kind enough to lend a hand.

    I display the latest five posts on a page external to my blog, however when you get a long post_title it can mess up the appearance. I would like to turn.

    “WordPress is a great tool for those who like to blog”
    into
    “WordPress is a great tool for those…”
    (cut off after 35 characters and insert “…”)

    Here is my current php code, which works (but displays full titles)

    <?php
    mysql_connect("******.******.******.***", "******_******", "******") or die(mysql_error());
    mysql_select_db("wordpress_blog") or die(mysql_error());
    
    $data = mysql_query("SELECT * FROM wp_posts WHERE post_status='publish' ORDER BY post_date DESC LIMIT 0, 5") or die(mysql_error()); while($info = mysql_fetch_array( $data )) 
    
    {
    Print "<font color='black' face='arial'><li><a href='/blog/?p=".$info['ID'] . "'>";
    Print $info['post_title'];
    Print "</a> <font color=#666666><i> - posted on ";
    $date = date('j M \'y', strtotime($info['post_date']));
    Print $date;
    Print "</i>";
    } 
    
     ?>

    The code that needs changing is:
    Print $info[‘post_title’];
    but to what I do not know.

    Hope somebody can help.

    Thanks

Viewing 9 replies - 1 through 9 (of 9 total)
  • well, you can restrict the post author for max words number in the title. this way you cont be having long title problems.

    add this code to your theme function.php file:

    function restrict_title_word_count($title) {
        global $post;
        $title = $post->post_title;
        if (str_word_count($title) >= 10 ) //set maximum number of words
        wp_die( __('Error: your post title is over the maximum word count.') );
    }
    add_action('publish_post', 'restrict_title_word_count');

    Source: http://www.wp-tricks.co.il/%D7%9C%D7%94%D7%92%D7%93%D7%99%D7%A8-%D7%9E%D7%A1%D7%A4%D7%A8-%D7%9E%D7%99%D7%9C%D7%99%D7%9D-%D7%9E%D7%A7%D7%A1%D7%99%D7%9E%D7%90%D7%9C%D7%99%D7%9E%D7%99%D7%A0%D7%99%D7%9E%D7%90%D7%9C%D7%99-%D7%9C/

    ReallyConfused

    (@reallyconfused)

    Hi Rami,

    Thanks for your reply.

    I have just the latest 5 posts in a small section on my main site page, and then a link to the actual blog itself (which is a full page wide, so no need to limit the length here)

    By limiting the author it would mean that the shortened post_title would appear on both pages, and while it would be perfect on the index page, it might look poor on the blog page.

    Ganners

    (@ganners)

    Just change Print $info[‘post_title’]; to something like

    $PostTitle = substr($info[‘post_title’], 0, 35);
    print $PostTitle;

    ReallyConfused

    (@reallyconfused)

    Hi Ganners,

    Thanks for your reply.

    This is more than half way to what I need. I does cut the title off at 35 characters, but fails to add “…” to the end of titles that have been truncated.

    Any ideas?

    Ganners

    (@ganners)

    Change print $PostTitle;

    with print $PostTitle. “…”;

    ReallyConfused

    (@reallyconfused)

    Haha, thanks again Ganners, I already thought of that but the problem it that it adds “…” to every title, even if it hasn’t been truncated.

    Ganners

    (@ganners)

    Sorry just realised after i posted! Replace it all with this:

    if ( strlen($info[‘post_title’]) > 35)
    {
    $PostTitle = substr($info[‘post_title’], 0, 35);
    print $PostTitle. “…”;
    }

    else
    {
    print $info[‘post_title’];
    }

    ReallyConfused

    (@reallyconfused)

    Ganners, thank you so much, it worked a treat.

    You really helped me out here, hope some good karma comes your way.

    Ganners

    (@ganners)

    No problem! Happy to help.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Cut off long post_title’ is closed to new replies.