I am customizing my WordPress site's theme and i need to use a different single.php file based on the condition.

Here is my user case, i need to design a competition page where i need to show entries submitted by the people.Entries can belong to various categories so it is not possible to trek them based on the categories.

I have created a tag (contest) and each entry belong to competition will be tagged with this tag, i can easily pick all entries based on this tag.

On the main navigation bar i have placed a link "Contest" which will take user to the contest page, on this page i am showing all submitted entries with short description and link to go to story page.

This is how i am displaying data on custom competition page

$ppp = get_option('posts_per_page');
$args = array(
    "tag" => getContestPageData('tag_name'),
    'posts_per_page' => 10,
    'post_type' => 'post',
    'orderby'=> 'date',
    'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1)
 foreach( $posts as $post ) : setup_postdata($post); ?>
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
       <img src="<?php echo $random_image[0]; ?>" alt="<?php the_title(); ?>" class="left"/>

moment user click on the anchor link it redirect user to the single.php page (if my understanding is correct)

I need Tweak main story page so as can design it with Competition theme in mind and i can do it with following option

  1. Put some conditional logic in single.php
  2. For all user who click story link on my custom page, land them on some other page say compition-single.php

Is this possible? please link to me appropriate resource or examples

  • So basically you want to know how to detect if the user has just come from a search listing? This sounds like you're trying to fix a solution, and you've not shown us the original problem. Perhaps if you explained why you want this and what you're trying to do that requires this?
    – Tom J Nowell
    Apr 11, 2013 at 14:57
  • 2
    Also your naming convention will cause issues, e.g. if you have a post with the ID 21, it will load single-21.php regardless of its category
    – Tom J Nowell
    Apr 11, 2013 at 14:57
  • @TomJNowell: well i am creating a page for some specific event and will show all event there, this even page is a custom page, so all i want is that if user click any post on this page it should use my custom.php page in place of single.php. I hope i am making some sense Apr 11, 2013 at 15:01
  • Can you not tag or categorise those posts and use the system you used above? Or use a custom post type?
    – Tom J Nowell
    Apr 11, 2013 at 15:10
  • @TomJNowell: can you explain this approach a bit, sorry i only having limited knowledge of wordpress Apr 11, 2013 at 15:12

1 Answer 1


In a previous edit you used:

if (in_category('21')) {
    include (TEMPLATEPATH . '/single-21.php');
} else {
    include (TEMPLATEPATH . '/single-29.php');

This is part of your solution, but before I continue, some important notes:

  • Don't use include like that, instead get_template_part('single-21'); works better
  • Indent correctly
  • This clashes with how IDs work in the template heirarchy. E.g. a post with ID 21 will always use single-21.php as its template

For your contest problem, what you want is:

if ( post has the tag 'contest' ) {

To determine if the post has the tag 'contest' use:

$terms = get_the_terms( get_the_ID(), 'tag' );
$is_contest = false;
foreach ( $terms as &$term ) {
    if ( $term->name == 'contest' ) {
        // Yes, this post has the contest tag
        $is_contest = true;

I'd recommend you read up on these:

  • register_post_type
  • register_taxonomy
  • get_template_part

I'd also strongly advise against hardcoding tags categories and numbers in your code, and that you get an editor that auto-indents your code, e.g. Sublime Text, PHPStorm, Netbeans, Komodo Edit, etc

Also as a last note, I strongly urge you not to use the query_posts function. Instead use WP_Query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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