WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Cut off long post_title (10 posts)

  1. ReallyConfused
    Member
    Posted 3 years ago #

    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

  2. Rami Yushuvaev
    Member
    Posted 3 years ago #

    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/

  3. ReallyConfused
    Member
    Posted 3 years ago #

    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.

  4. Ganners
    Member
    Posted 3 years ago #

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

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

  5. ReallyConfused
    Member
    Posted 3 years ago #

    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?

  6. Ganners
    Member
    Posted 3 years ago #

    Change print $PostTitle;

    with print $PostTitle. "...";

  7. ReallyConfused
    Member
    Posted 3 years ago #

    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.

  8. Ganners
    Member
    Posted 3 years ago #

    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'];
    }

  9. ReallyConfused
    Member
    Posted 3 years ago #

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

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

  10. Ganners
    Member
    Posted 3 years ago #

    No problem! Happy to help.

Topic Closed

This topic has been closed to new replies.

About this Topic