Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'd like to have the first paragraph/sentence of my posts in their single.php page styled differently - say... bold.

How do I go about doing that in the most efficient way?

share|improve this question
    
Explain why :first-line is not an option. –  toscho Jun 28 '12 at 19:35
    
two reasons: 1. didn't know it exist. 2. I need the first sentence. this only gets me a fragment of it. –  Tsahi Levent-Levi Jun 30 '12 at 11:22

2 Answers 2

Here is the idea

add_filter('the_content', 'testfunc');

function custom_formatter($matches){
  return "<strong>{$matches[0]}</strong>";
}

function testfunc($content)
{
  $pattern = "$\<p.*\>(.*)\<\/p\>$";
  $content = preg_replace_callback($pattern, 'custom_formatter', $content);
  return $content;
}

However, you will need a better regular expression in $pattern variable as this regex may be very poor. in the custom_formatter function you can do the formattings you want.

share|improve this answer
    
The above doesn't work. The pattern doesn't really catch the first paragraph. I worked something else out and posted it as an answer here. –  Tsahi Levent-Levi Jul 12 '12 at 18:51
up vote 0 down vote accepted

In the end, this is the function that worked for me:

add_filter('the_content', 'first_line_bold');
function first_line_bold($content)
{
    if (!is_page()) {
      $pattern = "/<p.*?>([^`]*?)<\/p>/";
      $match = "<span style=\"font-weight:bold;font-size:16px;\">$1</span>";
      $content = preg_replace($pattern, $match, $content, 1);
      return $content;
    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.