you need to take different post types into account. This covers at least three types, but probably also custom types. Also changes the template according to the type