Small hiccup on the “Featured” post

A few days back i posted on how to create a small place on top of your posts in your theme to keep a featured post there (just like the one running here, as you can see on top of the posts). Validating my blog today against different browsers i stumbled on a serious mistake i did. On that tutorial there is a line of code saying:

  1. <?=substr($featured->post_content, 0, 500);?> []

That has a serious bug that’s lurking around not ready to be found at all. This line shortens the post to the first 500 characters so it will appear like an excerpt. But here is the problem. When using a “more” tag to cut your posts the following text is inserted in the post:

  1. <!- – more – ->

The problem is when the cutting of the text stops just after the:

  1. <!- –

There is the problem. The above character sequence is the markup for HTML indicating that the text following is comments! There, the parsing stops. So, after that point your page will not render. That is a major problem as you can imagine. I have a fix for it though. Please replace the above line of code with this one:

  1. <?=substr(str_replace(‘<!–  more –>’, , $featured->post_content), 0, 500);?> []

This code makes an extra callto the str_replace function replacing the more tag with the empty string. This way, there is no way a problem like that can occur. Hope it didn’t happen to you guys because it’s scary! If you encounter any more problems please report them back to me!



5 Responses to “ “Small hiccup on the “Featured” post”

  1. […] Please check out this post for a small correction to the above […]

  2. I’m glad you found the error rather than someone else giving you a hard time about it.

    • stratosg says:

      @Kim Yah… I prefer finding out myself rather than someone else having hard time or even thinking that i have no idea of what i’m doing 🙂

  3. Sire says:

    Hey Stratos, did you know that version 2.7 comes with a ‘sticky’ feature that allows you allows you to select a featured post?

    Also I love your little ‘Hire Me’ post it note. I reckon that should work well for you.

    • stratosg says:

      @Sire Well i know it’s even easier to do it know. If you modify your theme a bit you get what i did for the featured post. But a) i did it when 2.7 was not out yet and b) i like my way of approach better 😛

      thanks for dropping by!

Leave a Reply