CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   Wordpress Help - truncating blog titles on listing page (http://www.codingforums.com/showthread.php?t=285574)

a4udi 01-10-2013 06:19 PM

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'  );
                
            
?>


TFlan 01-10-2013 06:26 PM

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

a4udi 01-10-2013 06:42 PM

Quote:

Originally Posted by TFlan (Post 1305333)
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.

TFlan 01-10-2013 06:56 PM

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 '...'

a4udi 01-10-2013 07:57 PM

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 
            
?>


TFlan 01-10-2013 08:53 PM

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)

a4udi 01-11-2013 01:00 AM

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!

TFlan 01-11-2013 02:37 PM

Anytime :), glad to help


All times are GMT +1. The time now is 12:34 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.