Add something to post title based on post type

Recently, I was working on a WordPress install employing several custom post types to handle two separate “blogs” within a single site. The site used normal ‘posts’ to handle articles and the client wanted to append the word “BLOG” to the beginning of blog posts’ titles only.

Using the Codex I slapped something together rather easily.

Using the following snippet, I built a simple if statement to test for blog posts, and if not, to render the_title() minus any extra text.

<br />
// First test for the post type(s)<br />
&lt;?php global $post<br />
if (get_post_type($post) == 'post_type_1') || get_post_type($post) == 'post_type_2') { ?&gt;<br />
// IF either test registers true, then set the $before parameter in this format: the_title($before, $after);<br />
&lt;h1&gt;&lt;a href=&quot;&lt;?php the_permalink(); ?&gt;&quot;&gt;&lt;?php the_title('BLOG: ', ''); ?&gt;&lt;/a&gt;&lt;/h1&gt;<br />
// IF not, then display as normal<br />
&lt;?php } else { ?&gt;<br />
&lt;h1&gt;&lt;a href=&quot;&lt;?php the_permalink(); ?&gt;&quot;&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/h1&gt;<br />
// Close the IF statement<br />
&lt;?php } ?&gt;<br />

Using this example, posts that register as true would display like this:

BLOG: The post title

Published by

Drew Jaynes

Drew is a formerCore Developer for the WordPress open source project, and he works on cool plugins like AffiliateWP, Easy Digital Downloads, and Restrict Content Pro.

Leave a Reply