Hi Guys, I've just set up a new Wordpress Blog & am having problems with the title length of some of the stuff I'm pulling in from Amazon as I also wish to feed these post titles & urls to Twitter (140 character limit). Anyway I've tried for a few days to modify the content_limit which is already part of my theme, to work as a title_limit, but unfortunately my php skills are only slightly better than useless! Ideally what I would like to achieve is a method of limiting the length of the titles, without breaking the permalinks which are in a h2 tag & also if possible truncating the title at the space between words, so at least it will still make partial sense. I'd be very grateful if anyone could offer me some advice on this as it's driving me nuts at the moment! Thanks Steve
http://php.net/manual/en/function.substr.php $title = "Your title"; $maxLength = 30; substr($title, 0, $maxLength); PHP:
Hi Guys, thanks for the help & sorry for the delay in getting back to you, but I've been away for a couple of days working, anyway this is the code I am trying to sort out - <?php while (have_posts()) : the_post(); ?> <div id="archive"> <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"> <?php the_title(); ?> </a> </h2> <div class="left"><a href="<?php the_permalink() ?>" rel="bookmark"><img src="<?php bloginfo('template_directory'); ?>/includes/timthumb.php?src=<?php get_thumbnail($post->ID, 'full'); ?>&h=100&w=100&zc=1" alt="<?php the_title(); ?>" /></a></div> <div class="archiveright"> <?php the_content_limit(400,''); ?> </div> <div class="clear"></div> PHP: It is the title within the H2 tags that is causing the problems & unfortunately I have no control over which products are selected for posting on my site, therefore as I already have a section of code in the functions.php which is limiting the content of each product description to 400 characters, I thought that perhgaps it would be possible to limit the post title in a similar way..?? Thanks again Steve
<?php while (have_posts()) : the_post(); ?> <div id="archive"> <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"> <?php echo substr(the_title(), 0, 140); ?> </a> </h2> <div class="left"><a href="<?php the_permalink() ?>" rel="bookmark"><img src="<?php bloginfo('template_directory'); ?>/includes/timthumb.php?src=<?php get_thumbnail($post->ID, 'full'); ?>&h=100&w=100&zc=1" alt="<?php the_title(); ?>" /></a></div> <div class="archiveright"> <?php the_content_limit(400,''); ?> </div> <div class="clear"></div> PHP: this would limit it to 140 chars in title
Hey G3n3s!s, thanks man that actually worked ! But there is one minor problem with it, it cuts the words if the limit falls in the middle of a word Is there a fairly easy way for me to correct this ? Thanks for your help at least I have something to work on now anyway.
I am not sure if it will work but try it. It should type TITLEN.... if it has more than 140 chars. <?php while (have_posts()) : the_post(); ?> <div id="archive"> <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"> <?php $s = substr(the_title(), 0, 140); if (strlen(the_title()) > 140) $dot = "..."; echo substr(the_title(), 0, 140).$dot; ?> </a> </h2> <div class="left"><a href="<?php the_permalink() ?>" rel="bookmark"><img src="<?php bloginfo('template_directory'); ?>/includes/timthumb.php?src=<?php get_thumbnail($post->ID, 'full'); ?>&h=100&w=100&zc=1" alt="<?php the_title(); ?>" /></a></div> <div class="archiveright"> <?php the_content_limit(400,''); ?> </div> <div class="clear"></div> PHP:
there is add reputation button forums.digitalpoint.com/reputation.php?do=addreputation&p=15664965 BTW, if you want to ask something do not hesitate to PM me/reply here
Well Guys I got my test blog setup & tried out your suggestions, but unfortunately I couldn't get either method to work! However I have found another piece of code; <h2> <a href="<?php the_permalink() ?>"> <?php $thetitle = get_the_title(); $getlength = strlen($thetitle); $thelength = 80; echo substr($thetitle, 0, strrpos( substr( $thetitle, 0, 80), ' ' ) ); if ($getlength > $thelength) echo "..."; ?> </a> </h2> PHP: Which does actually truncate the title prior to display on the screen, the problem I have now is that I need this code to actually replace the original title & permalink with the truncated version. Any help would be fantastic thanks