WordPress.org

Ready to get started?Download WordPress

Forums

Alternate single post template for specific categories (14 posts)

  1. Mithrustt
    Member
    Posted 6 years ago #

    OK, so I've figured out most of this, I think, but I need some help implementing. I want it so when a specific category is posted in, it uses an alternate single post template that I've called singlereview.php. So, I went into the codex and found this bit of code and added in my category IDs:

    <?php
     if ( have_posts() ) { the_post(); rewind_posts(); }
     if ( in_category(25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 38) ) {
     include(TEMPLATEPATH . '/singlereview.php');
     } else {
     include(TEMPLATEPATH . '/single.php');
     }
     ?>

    However, I'm having trouble figuring out where in the single.php to put it and if I have to replace other code with it or what.

    This is my single.php:

    <?php get_header(); ?>
    
            <div id="topbanner_single" class="column span-14">   <!-- start top banner -->
    
            </div>   <!-- end top banner -->
    
            <div id="post_content" class="column span-14">   <!-- start home_content -->
    
            <?php if (have_posts()) : ?>
    
            <?php while (have_posts()) : the_post(); ?>
    
            	<div class="column span-11 first">
    
                    <div id="post_header">
    
                    <div class="post_title" id="post-<?php the_ID(); ?>"><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></div>
    
    <div class="post_info">
                		By <?php the_author_posts_link(); ?> <span class="dot">&sdot;</span> <?php the_time('F j, Y'); ?> <span class="dot">&sdot;</span> <?php the_category(', '); ?>  <strong>&nbsp;</strong><?php edit_post_link('edit','','<strong></strong>'); ?>
                	</div>
                    </div>
    
    <div id="post">
    
                	<?php the_content('<p>Continue reading this post</p>'); ?>
    
    <div id="post_footer">
    	<div class="post_meta">
    						<!-- AddThis Button BEGIN -->
    <a href="http://www.addthis.com/bookmark.php" onclick="window.open('http://www.addthis.com/bookmark.php?wt=nw&pub=Saving Progress&url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title), 'addthis', 'scrollbars=yes,menubar=no,width=620,height=520,resizable=yes,toolbar=no,location=no,status=no,screenX=200,screenY=100,left=200,top=100'); return false;" title="Bookmark and Share" target="_blank"><img src="http://s9.addthis.com/button1-addthis.gif" class='addthis' width="125" height="16" border="0" alt="Bookmark and Share" /></a>
    <!-- AddThis Button END --> <span class="dot">&sdot;</span> <?php if(function_exists('wp_email')) { ?> <?php email_link(); ?> <span class="dot">&sdot;</span> <?php } ?> <?php if(function_exists('wp_print')) { ?> <?php print_link(); ?><?php } ?>
    					</div>
    </div>
    </div>
    
    				<?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
    
                            <div class="discussion_header">
    
    <div class="comments_for"><?php comments_number('no comments', '1 comment so far', '% comments so far'); ?></div>
    
                           </div>
    
    				<div id="comments">   <!-- start comments -->
    
    <?php comments_template(); ?>
    
    				</div>		
    
    					   <!-- end comments -->
    
                </div>
    
            <?php endwhile; else: ?>
    
    		<p>Lost? Go back to the <a href="<?php echo get_option('home'); ?>/">home page</a>.</p>
    
    		<?php endif; ?>    
    
                <?php get_sidebar(); ?>     
    
            </div>   <!-- start home_content -->
    
    <?php get_footer(); ?>
  2. Mithrustt
    Member
    Posted 6 years ago #

    OK, I've switched that first bit of code with this, however, I still don't know where I should be putting it, as everywhere I've tried messes stuff up.

    <?php
    $post = $wp_query->post;
    if ( in_category('25') ) {
       include(TEMPLATEPATH . '/singlereview.php');
    } else {
       include(TEMPLATEPATH . '/single.php');
    }
    ?>
  3. shane10101
    Member
    Posted 6 years ago #

    I'm trying to do much the same thing, though with just one category. I tried a plugin called "Post Templates By Category", but it's really buggy in 2.5.1 -- it made all the URLs to posts in the category I'd selected, point to the most recent post in that category. We-eird.

    I guess I could just use a giant "if...then...else" conditional, & have the code for one whole template inside the "then" part, and the code for the other template inside the "else" part, but that would make me feel really dirty.

    I'll start digging. Somebody stop me if you know how to do this.

    Thanks!

    Shane

  4. shane10101
    Member
    Posted 6 years ago #

    okay, this plugin causes the exact same error. (I guess that's somewhat reasonable.)

  5. shane10101
    Member
    Posted 6 years ago #

    oops -- it was my freakin' template that was causing the error. Should've known.

    Mithrustt, I'll see if I can give you an answer in just a minute.

  6. shane10101
    Member
    Posted 6 years ago #

    Okay, Mitthrustt -- Using your original code, I think you'll need to do something like this:

    First, I don't think in_category() will take a list of values. You'll have to use the php "or" operator, like this: "...in_category(1) || in_category(2) || in_category(3)..."

    You'll need three separate template files; for instance: single.php, singlereview.php, and single-default.php

    Assuming you only want to modify what goes on between the header & footer, your single.php should look like this:

    <?php get_header(); ?>
    
    	<?php
            if ( have_posts() ) { the_post(); rewind_posts(); }
            if ( in_category(25) || in_category(26) || in_category (27) ) {
                include(TEMPLATEPATH . '/singlereview.php');
            }
            else {
                include(TEMPLATEPATH . '/single-default.php');
            }
        ?>
    
    <?php get_footer(); ?>

    ...and your single-default & singlereview files should contain everything that originally went between the get_header and get_footer tags in your original single.php file, modified however you'd like.

    Or, you could try one of the two plugins that I linked to previously, now that I know it was my template that was screwey, & (probably) not the plug-ins.

    Happy coding!

    Shane

  7. Mithrustt
    Member
    Posted 6 years ago #

    Yeah, I figured this out a few days ago and forgot to post back saying how.

    Basically, I took the single.php and put this code in:

    <?php
     if ( have_posts() ) { the_post(); rewind_posts(); }
     if ( in_category(25) || in_category (26) || in_category (27) || in_category (28) || in_category (29) || in_category (30) || in_category (31) || in_category (32) || in_category (33) || in_category (34) || in_category (35) || in_category (36) || in_category (38) ) {
     include(TEMPLATEPATH . '/singlereview.php');
     } else {
     include(TEMPLATEPATH . '/singlepost.php');
     }
     ?>

    Then I just created the two new templates and styled them the way I desired. Quite easy, actually, and once you get the basic code for one alternate template, it's really easy to do it for more.

    I hope this helps anyone else looking for an answer to a similar question.

  8. RoseCitySister
    Member
    Posted 6 years ago #

    It certainly helped me, and I didn't even know I had the question! :) Thanks, good for future reference.

  9. Eric Amundson
    Volunteer Moderator
    Posted 6 years ago #

    Awesome! Thanks for posting this guys. Helped me out too!

  10. nathanrice
    Member
    Posted 6 years ago #

    There's a better method for this that what's above. I wrote this a while ago:
    http://www.nathanrice.net/blog/wordpress-single-post-templates/

  11. shane10101
    Member
    Posted 6 years ago #

    Thanks, Nathan! I'm glad people found this handy, but I figured there was a more elegant way of handling it. Looks good.

    Always Learning!

    Shane

  12. jayo
    Member
    Posted 6 years ago #

    Hey guys, this is a solution that worked for me perfectly. All I did is at this code to my single.php template

    <?php
    $post = $wp_query->post;
    if (in_category(9)) {
    	include (TEMPLATEPATH.'/single-specific9.php');
    	return;
    }
    if (in_category(8)) {
    	include (TEMPLATEPATH.'/single-specific8.php');
    	return;
    }
    if (in_category(11)) {
    	include (TEMPLATEPATH.'/single-specific11.php');
    	return;
    }
    get_header(); ?>

    Hopefully this can help you.

  13. taghaboy
    Member
    Posted 5 years ago #

    Thanks Guys for sharing your knowledg,
    her another code i use , and it work perfectly

    <?php
    $post = $wp_query->post;
    
    if (in_category('news')) {
    	include ('news.php');
    	return;
    
    } else { ?>
    	<?php include ('other.php');?>
    <?php } ?>

    Hop it help.

  14. redsoxmaniac
    Member
    Posted 5 years ago #

    Would it be possible to call for a template of a specific post using the custom fields?

    So instead of using a category, it would only work for the post. I would like this, because my writing sense isn't based on category, more on the utility of the article itself. Something related to page templates with pages would be good. Any code around?

Topic Closed

This topic has been closed to new replies.

About this Topic