Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-10-2013, 06:19 PM   PM User | #1
a4udi
Regular Coder

 
Join Date: Jan 2005
Posts: 181
Thanks: 5
Thanked 0 Times in 0 Posts
a4udi is an unknown quantity at this point
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'  );
                
            
?>
a4udi is offline   Reply With Quote
Old 01-10-2013, 06:26 PM   PM User | #2
TFlan
New Coder

 
Join Date: Dec 2012
Location: USA
Posts: 82
Thanks: 3
Thanked 17 Times in 17 Posts
TFlan is an unknown quantity at this point
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..
TFlan is offline   Reply With Quote
Old 01-10-2013, 06:42 PM   PM User | #3
a4udi
Regular Coder

 
Join Date: Jan 2005
Posts: 181
Thanks: 5
Thanked 0 Times in 0 Posts
a4udi is an unknown quantity at this point
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.
a4udi is offline   Reply With Quote
Old 01-10-2013, 06:56 PM   PM User | #4
TFlan
New Coder

 
Join Date: Dec 2012
Location: USA
Posts: 82
Thanks: 3
Thanked 17 Times in 17 Posts
TFlan is an unknown quantity at this point
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 '...'
TFlan is offline   Reply With Quote
Old 01-10-2013, 07:57 PM   PM User | #5
a4udi
Regular Coder

 
Join Date: Jan 2005
Posts: 181
Thanks: 5
Thanked 0 Times in 0 Posts
a4udi is an unknown quantity at this point
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 
            
?>
a4udi is offline   Reply With Quote
Old 01-10-2013, 08:53 PM   PM User | #6
TFlan
New Coder

 
Join Date: Dec 2012
Location: USA
Posts: 82
Thanks: 3
Thanked 17 Times in 17 Posts
TFlan is an unknown quantity at this point
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..
TFlan is offline   Reply With Quote
Old 01-11-2013, 01:00 AM   PM User | #7
a4udi
Regular Coder

 
Join Date: Jan 2005
Posts: 181
Thanks: 5
Thanked 0 Times in 0 Posts
a4udi is an unknown quantity at this point
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!
a4udi is offline   Reply With Quote
Old 01-11-2013, 02:37 PM   PM User | #8
TFlan
New Coder

 
Join Date: Dec 2012
Location: USA
Posts: 82
Thanks: 3
Thanked 17 Times in 17 Posts
TFlan is an unknown quantity at this point
Anytime , glad to help
TFlan is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:06 PM.


Advertisement
Log in to turn off these ads.