Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Jan 2005
    Posts
    189
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Wordpress Help - truncating blog titles on listing page

    This should be pretty simple but I seem to be having some issue with Wordpress. I would just like any title longer than 50 characters to be cut off and end with "..." Here is the code I currently have: Nothing is happening, the page works but it' doesn't truncate the post title string.

    PHP Code:
                <?php 
                $post_obj 
    $wp_query->get_queried_object();
                
    $post_ID $post_obj->ID;        
                
    // ORIGINAL CODE THAT GRABS THE POST TITLE ---  $post_title = $post_obj->post_title;
                    
                // code added to truncate blog posts longer than 50 characers
                
    $post_title_fetch $post_obj->post_title;
                
                if (
    strlen($post_title_fetch) > 50) {     
                
    $post_title substr($post_title_fetch050);
                echo 
    '...';    
                } else {
                
    $post_title $post_title_fetch;
                }
                
    // end truncate code
            
                
    $post_name $post_obj->post_name;
                    if(
    $post_name == 'blog') {
                        
    $page get_page_by_title'Blog Intro' );
                        
    $pgid $page->ID;
                        
    $page_data get_page$pgid );

                        
    $content apply_filters('the_content'$page_data->post_content);
                        
    $title $page_data->post_title;    
                        echo 
    '<div class="bltr">'$content .'</div>';
                    }
                
                 
    get_template_part'loop'  );
                    
                
    ?>

  • #2
    New Coder
    Join Date
    Dec 2012
    Location
    USA
    Posts
    82
    Thanks
    3
    Thanked 17 Times in 17 Posts
    Looks like you are just forgetting to reassign the title back to the original object.

    PHP Code:
    $post_obj->post_title $post_title
    Place that line after your truncating


    Edit:

    Also I don't know which title you are trying to post.. You have two "post_title"'s from two different classes
    Last edited by TFlan; 01-10-2013 at 06:30 PM.

  • #3
    Regular Coder
    Join Date
    Jan 2005
    Posts
    189
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by TFlan View Post
    Also I don't know which title you are trying to post.. You have two "post_title"'s from two different classes
    That didn't seem to work. Maybe I should clarify a bit.

    It is a blog index page, so it loops and posts the most recent 5 stories, not one particular title. So that code is in a look and I just want to check for any title over 50 characters long. I don't think that part should make a difference though, there just seems to be something wrong with the way it's grabbing the "post_title" variable.

  • #4
    New Coder
    Join Date
    Dec 2012
    Location
    USA
    Posts
    82
    Thanks
    3
    Thanked 17 Times in 17 Posts
    You misunderstood me

    You have two "post_title" variables.

    PHP Code:
    $post_obj->post_title;
    $page_data->post_title
    You are only truncating $post_obj->post_title; (without reassigning the truncated value to the title for some reason)

    Also, I see no code printing any post titles? only the page content and '...'

  • #5
    Regular Coder
    Join Date
    Jan 2005
    Posts
    189
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Sorry, I am editing this for someone else and just realize it was totally in the wrong section.

    Here is the actual code printing the Title's

    PHP Code:
            <a href="<?php the_permalink(); ?>">
                <h1><?php the_title(); ?></h1>
            </a>
    I tried editing like this, but doesn't seem to work still. Can I applly substring to a function like 'the_title()' ??

    PHP Code:
                <?php 
                
    if (strlen(the_title()) > 50) { ?>
                    <a href="<?php the_permalink(); ?>">
                    <h1><?php substr(the_title(), 050); ?>...</h1>
                    </a>
                <?php 
                
    } else { 
                
    ?>
                    <a href="<?php the_permalink(); ?>">
                    <h1><?php the_title(); ?></h1>
                    </a>
                <?php 
                
    ?>

  • #6
    New Coder
    Join Date
    Dec 2012
    Location
    USA
    Posts
    82
    Thanks
    3
    Thanked 17 Times in 17 Posts
    It depends on whether the function "the_title()" is returning or echoing the title.

    If it is returning the title then yes, you can apply the substr function. If it is echoing the title then no, you can not apply the substr function.

    You need to look for and find that function.

    (I have a feeling it is echoing the title)
    Last edited by TFlan; 01-10-2013 at 09:02 PM.

  • #7
    Regular Coder
    Join Date
    Jan 2005
    Posts
    189
    Thanks
    6
    Thanked 0 Times in 0 Posts
    You are correct! It was echoing... I found the function in the post_template file and wrote a new one to handle truncating the titles! Thanks for your guidance, it helped me solve this!

  • #8
    New Coder
    Join Date
    Dec 2012
    Location
    USA
    Posts
    82
    Thanks
    3
    Thanked 17 Times in 17 Posts
    Anytime , glad to help


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •