Will Oller .com

Avatar

Learning to match the beat of the Old World man.

Limiting title and excerpt length with Wordpress

Sometimes clients have very specific requirements. Sometimes they want autometic controls on simple things they have total control over anyway. I digress.

In this case, blog titles needed to be of any length, but have their lengths truncated under certain circumstances. Furthermore, excerpts need to be able to be the default length (50 words), but shorter (10 words) under those same certain circumstances.

Fortunately for me, the excerpt was easily fixed using a plugin called the excerpt reloaded.

http://guff.szub.net/2005/02/26/the-excerpt-reloaded/

Next, for the title length requirements, you will need to modifiy a core file: wp-includes/post-template.php.

PHP:
  1. function the_title($before = '', $after = '', $echo = true, $length = false) {
  2.    $title = get_the_title();
  3.     if ( $length && is_numeric($length) ) {
  4.        $title = substr( $title, 0, $length );
  5.     }
  6.     if ( strlen($title)> 0 ) {
  7.        $title = apply_filters('the_title', $before . $title . $after, $before, $after);
  8.        if ( $echo )
  9.           echo $title;
  10.        else
  11.           return $title;
  12.     }
  13. }

Finally, when in The Loop, call the_title() like this:

PHP:
  1. <?php the_title('', '', true, '40') ?>

Now, the title will be a mere 40 characters long, and the excerpt (using the_excerpt_reloaded()) can be any desired length.

CSS Sibling selectors

For those of us who care about the semantic web, standards, or the W3C, The Web Standards Project needs to be in your feed reader. I'll admt, it wasn't in mine; until now that is.

The design of the site is awesome, and they eat their own cooking. (Plus, they use Wordpress!) They employ css tricks I have seldom seen used before, and consistently use semantic tags (<abbr>, <q>).

The best trick of all: the sibling selector. Why? Because it allows for truly semantic output with the leanest markup possible. Before, I might have marked up a simple content block like this:


<div class="content">
   <h1>Title</h1>
   <img src="url" alt="pic related to story">
   <div class="block">
      <p class="intro">This is an introduction sentance or paragraph.  It is supposed to represent a blurb about the article below.</p>
      <div class="mainbody">
         <p>content</p>
         <p>content</p>
         <p>content</p>
         <p>content</p>
      </div>
      <p class="footer">Footer.  Date or links, or whatever.</p>
   </div>
</div>

This way, all of the elements have class hooks. CSS is applied using the classes and simple hierarchies:


div.content { }
div.content h1 { font-size:1.8em; }
div.content div.block { border:1px solid black; }
div.block p.intro { font-weight: bold; }
div.block img { float:right; }
div.block div.mainbody { clear: both; }
div.mainbody p { color: #006; }
p.footer { font-size: 0.7em; color: #888; }

Class hooks are helpful, but not really needed if you use sibling selectors! You, too, can write super-styled code without the additional markup headache!


<div class="content" >
   <h1>Title</h1>
   <img src="url" alt="That image again"/>
   <p>This is an introduction sentance or paragraph.  It is supposed to represent a blurb about the article below.</p>
   <p>content</p>
   <p>content</p>
   <p>content</p>
   <p>content</p>
   <p class="footer">Footer.  Date or links, or whatever.</p>
</div>

See, that code is cleaner and easier to maintain. The CSS required to


div.content { }
div.content h1 { font-size:1.8em; }
div.content p { color: #006; }
div.content h1+p { color: #000; font-weight: bold; clear:both;}
div.content h1+img { float:right; }
div.content img { float:left } /* Just for fun */
div.content p.footer { font-size: 0.7em; color: #888; }


That's all there is to it.  Now, the line between content and design is darkened.

One thing: the class hook remains on the footer element.  This is because adjacent-sibling selectors do not work in reverse.  The selector applies only to the element after the +, and there is no selector that applies to a previous adjacent-sibling element.

Sources

Eric Myer wrote about them. The W3C recommends them. John Gallant and Holly Bergevin say IE7 supports them.

Continue