WordPress.org

Ready to get started?Download WordPress

Forums

singleX.php using is_single doubling up (3 posts)

  1. silverwing
    Member
    Posted 8 years ago #

    I'm modifying the default theme (in 1.5.2) to use different single.php for a couple of categories. However, when I view a post in that category, I get the post as expected but under the footer I get this: "Sorry, no posts matched your criteria" and the sidebar repeats (but not the footer).

    This only happens on posts, not on Pages, archives, search.

    Is there something in single.php I need to remove to get this doubling up taken care of?

    My single.php is this:

    <?php
    $post = $wp_query->post;
    if ( in_category('1') ) {
    include(TEMPLATEPATH . '/singleone.php'); }
    if ( in_category('2') ) {
    include(TEMPLATEPATH . '/singletwo.php'); }
    else {
    include(TEMPLATEPATH . '/singledefault.php');
    }
    ?>

    And my singleone.php etc have all the same code as the default single.php, slightly altered, i.e.
    <div id="content" class="narrowcolumn">

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    ----snip---
    <div class="post" id="post-<?php the_ID(); ?>">
    ---snip---
    <?php comments_template(); ?>

    <?php endwhile; else: ?>

    Sorry, no posts matched your criteria.

    <?php endif; ?>

    </div></div>
    <?php include (TEMPLATEPATH . '/sidebarsingle.php'); ?>
    <?php get_footer(); ?>

  2. Kafkaesqui

    Posted 8 years ago #

    The logic of your code in single.php is a little off:

    <?php
    $post = $wp_query->post;
    if ( in_category('1') ) {
    include(TEMPLATEPATH . '/singleone.php'); }
    elseif ( in_category('2') ) {
    include(TEMPLATEPATH . '/singletwo.php'); }
    else {
    include(TEMPLATEPATH . '/singledefault.php');
    }
    ?>

    Note the change to elseif in the second block. You also shouldn't need the $post = $wp_query->post;

    Anyway, you may be interested in a plugin of mine:

    http://guff.szub.net/post-templates-by-category/

  3. silverwing
    Member
    Posted 8 years ago #

    the 'elseif' is what I needed.

    thanks, everyone.

Topic Closed

This topic has been closed to new replies.

About this Topic