WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Truncate Titles in Recent Posts Widget? (11 posts)

  1. justbishop
    Member
    Posted 4 years ago #

    I've searched and searched, but am not finding an answer to this...is there any way to truncate the post titles in the default Recent Posts widget to a certain number of characters?

  2. Mark / t31os
    Moderator
    Posted 4 years ago #

    In the default one, no i don't think so....

    Suggestion: Take the code from the widget, change a few of the names around (so there's no clashing) and register it as your own widget, you can then apply any funky PHP you like, such as truncating titles.

  3. justbishop
    Member
    Posted 4 years ago #

    Hmmm...that's probably just a little too over my head to be doable at the moment :(

  4. Mark / t31os
    Moderator
    Posted 4 years ago #

    Well here's how you'd do it, the only piece i've left out is how to truncate the titles, as i have no idea what length or amount of words you're aiming for..

    http://wordpress.pastebin.com/YzzNSyRv

    What the code above does is unregister the original recent comments widget, then registers a new one, under a slightly different name, but utilises the settings from the original widget (so you don't have to do anything in the admin, it will update your existing recent comments widget).

    All that's left is to customise the code, i've put some comments by the area that needs modifying.. which looks like so..

    // YOUR TITLE TRUNCATING CODE GOES HERE

    The code can go in your functions file (your theme's), or alternatively i can convert it into a plugin for you (so you can download and activate, if you find that easier).

    I'll also help with truncating the titles if you let me know the specifics of how they should be truncated... ie. what length or word count ..

    :)

  5. justbishop
    Member
    Posted 4 years ago #

    Yeah, so you are freakin' awesome! Thank you so much!

    I combined your so generously provided code with THIS, and got it working exactly as I needed it to!

    And just a suggestion, but your should totally make this into a proper plugin, maybe adding an option in the widget to specify the character count. I found NOTHING that would do this, and I'm sure it's a feature that lots of people would like to have :)

  6. Mark / t31os
    Moderator
    Posted 4 years ago #

    Happy to help.. :)

    Code is there for anyone to mod if they wish to expand functionality... :)

  7. lelebart
    Member
    Posted 4 years ago #

    great! I need something similar for links widget and bbpress discussions (this plugin).. for the plugin I'll take the code from it and try to modify, but regarding the links widget I really don't know how/where to start..

  8. Mark / t31os
    Moderator
    Posted 4 years ago #

    If you get stuck, post a new thread describing what you're trying to do, what you've got so far, and where you need help..

    Then shout my name 100 times and i'll appear like magic to help.. :)

    Ok, i was kidding about shouting my name, but i'll give you a hand if i spot the thread.. ;)

  9. lelebart
    Member
    Posted 3 years ago #

    nevermind, i didn't get the options' page of the plugin-in for bbpress topics.. shame on me :/

    however i get a Fatal error: Call to undefined function wp_reset_postdata() because i'm using the to-be-updated WordPress 2.9.2, right? can i simply "comment" it?

    edit: solved right now as below ^^

    // Reset the global $the_post as this query will have stomped on it
    if (!function_exists('wp_reset_postdata')) { //for wp < 3.0 by lelebart
    function wp_reset_postdata() {
    global $wp_query;
    if ( !empty($wp_query->post) ) {
    $GLOBALS['post'] = $wp_query->post;
    setup_postdata($wp_query->post);
    }
    }
    }
    wp_reset_postdata();

  10. safeincome
    Member
    Posted 3 years ago #

    Help, Mark / t31os!

    Thank you very much for this nice codes, It's a while that I was looking for it. But, I'm very new to coding! May you please explain what do you mean of // YOUR TITLE TRUNCATING CODE GOES HERE. I want the max characters of 20 in the post titles in the default Recent Posts widget, which I'm using in sidebar.
    I tried to replace "// YOUR TITLE .." with a code! but It bounced a fatal error. what I did: from this code:
    <?php echo 'shortened title ' .substr($post->post_title,0,20) ; ?>

    copied :
    echo 'shortened title ' .substr($post->post_title,0,20) ;

    and pasted it in place of // YOUR TITLE TRUNCATING CODE GOES HERE.

    I know that to you it seems funny.
    Please, tell me how can I get it done? what is my truncating code? exactly where should I insert it? Thanks a lot.

  11. safeincome
    Member
    Posted 3 years ago #

    Anybody! Help please!

Topic Closed

This topic has been closed to new replies.

About this Topic