Title: need code help creating conditional archive template
Last modified: August 19, 2016

---

# need code help creating conditional archive template

 *  Resolved [wildbug](https://wordpress.org/support/users/wildbug/)
 * (@wildbug)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/need-code-help-creating-conditional-archive-template-2/)
 * I’m not a programmer but have picked up a small bit of stuff. I’m trying to create
   an archive template that shows only the clickable post title for a certain group
   of categories and for the rest, the thumbnail attachment, title, and excerpt (
   this is what it currently displays).
 * Is it best to create two separate archive templates and direct the template path
   based on categories, or to create 1 conditional archive template?
 * I tried both without complete success.
 * I used this code in archive.php:
 *     ```
       <?php
        if ( have_posts() ) { the_post(); rewind_posts(); }
        if ( in_category(4,5,6,7,8,9,10,453,454,455,456,457,458,459,460) ) {
        include(TEMPLATEPATH . '/archivesexperts.php');
        } else {
        include(TEMPLATEPATH . '/archives.php');
        }
        ?>
       ```
   
 * but it doesn’t seem to be working. Can anyone tell me if they see any problems
   with this code?
 * I also tried using 1 conditional archive template as shown below:
 *     ```
       <?php get_header(); ?>
   
       <!-- BEGIN content -->
       <div id="content">
   
       	<!-- BEGIN recent posts -->
       	<div class="span cbox arc">
       	<?php if (have_posts()) : $count = 0; ?>
       		<?php /* If this is a category archive */ if (is_category(4,5,6,7,8,9,10,453,454,455,456,457,458,459,460)) { ?>
       		<h2><?php fs_lang("Archive for "); ?> <strong><?php single_cat_title(); ?></strong></h2>
       		<?php } ?>
       		<ul>
       		<?php while (have_posts()) : the_post(); $count++; ?>
       		<li<?php if ($count==1) echo ' class="first"'; ?>>
       			<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
       		</li>
       		<?php endwhile; ?>
       		</ul>
       		<div class="postnav">
       			<div class="l"><?php next_posts_link('&laquo; '.fs_lang('Older Entries', false)); ?></div>
       			<div class="r"><?php previous_posts_link(fs_lang('Newer Entries', false).' &raquo;'); ?></div>
       		</div>
   
       	<?php elseif (have_posts()) : $count = 0; ?>
       		<?php $post = $posts[0]; // Hack. Set $post so that the_date() works. ?>
       		<?php /* If this is a category archive */ if (is_category()) { ?>
       		<h2><?php fs_lang("Archive for "); ?> <strong><?php single_cat_title(); ?></strong></h2>
       		<?php /* If this is a tag archive */ } elseif( is_tag() ) { ?>
       		<h2><?php fs_lang("Posts Tagged "); ?> <strong><?php single_tag_title(); ?></strong></h2>
       		<?php /* If this is a daily archive */ } elseif (is_day()) { ?>
       		<h2><?php fs_lang("Archive for "); ?> <?php the_time('F jS, Y'); ?></h2>
       		<?php /* If this is a monthly archive */ } elseif (is_month()) { ?>
       		<h2><?php fs_lang("Archive for "); ?> <?php the_time('F, Y'); ?></h2>
       		<?php /* If this is a yearly archive */ } elseif (is_year()) { ?>
       		<h2><?php fs_lang("Archive for "); ?> <?php the_time('Y'); ?></h2>
       		<?php /* If this is an author archive */ } elseif (is_author()) { ?>
       		<h2><?php fs_lang("Author Archive"); ?> </h2>
       		<?php /* If this is a paged archive */ } elseif (isset($_GET['paged']) && !empty($_GET['paged'])) { ?>
       		<h2><?php fs_lang("Blog Archives"); ?></h2>
       		<?php } ?>
       		<ul>
       		<?php while (have_posts()) : the_post(); $count++; ?>
       		<li<?php if ($count==1) echo ' class="first"'; ?>>
       			<a class="attachment" href="<?php the_permalink(); ?>"><?php fs_attachment_image($post->ID, 'thumbnail', 'alt="' . $post->post_title . '"'); ?></a>
       			<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
       			<p><?php echo fs_clean($post->post_excerpt); ?></p>
       		</li>
       		<?php endwhile; ?>
       		</ul>
       		<div class="postnav">
       			<div class="l"><?php next_posts_link('&laquo; '.fs_lang('Older Entries', false)); ?></div>
       			<div class="r"><?php previous_posts_link(fs_lang('Newer Entries', false).' &raquo;'); ?></div>
       		</div>
       	<?php else: ?>
       		<h2><?php fs_lang("Not Found"); ?></h2>
       		<p><?php fs_lang("You are searching for something that isn't here."); ?></p>
       	<?php endif; ?>
       	</div>
       	<!-- END recent posts -->
   
       </div>
       <!-- END content -->
   
       <?php get_sidebar(); get_footer(); ?>
       ```
   
 * but with the above code, I see these problems:
    • ALL category archive pages 
   missing “ARCHIVES FOR [CATEGORY NAME]” title  • “If” categories are correctly
   showing post titles only but “Else if” categories are as well when they should
   be showing the image attachment and excerpt as well
 * Can someone tell me how to fix either of the above options to work properly?
 * THANK YOU!!

Viewing 6 replies - 1 through 6 (of 6 total)

 *  [thenameisnick](https://wordpress.org/support/users/thenameisnick/)
 * (@thenameisnick)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/need-code-help-creating-conditional-archive-template-2/#post-1211397)
 * Whoa….way too crazy. Remember the old adage, KISS, Keep It Simple Stupid.
 * In your archive,
 *     ```
       <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
   
       <?php if (is_category(array(1,2,3,etc))) { ?>
       Do stuff
       <?php } else if (is_category()) { ?>
       Do other stuff
       <?php } ?>
   
       <?php endwhile; ?>
       Do Stuff
       <?php else: ?>
       Do Stuff
       <?php endif; ?>
       ```
   
 * Make sense? Let me know.
 *  Thread Starter [wildbug](https://wordpress.org/support/users/wildbug/)
 * (@wildbug)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/need-code-help-creating-conditional-archive-template-2/#post-1211548)
 * Actually, I had tried to follow the KISS approach, I just don’t know how to execute
   it! 😉
 * This is the code in the archive.php that came with my theme:
 *     ```
       <?php get_header(); ?>
   
       <!-- BEGIN content -->
       <div id="content">
   
       	<!-- BEGIN recent posts -->
       	<div class="span cbox arc">
       	<?php if (have_posts()) : $count = 0; ?>
       		<?php $post = $posts[0]; // Hack. Set $post so that the_date() works. ?>
       		<?php /* If this is a category archive */ if (is_category()) { ?>
       		<h2><?php fs_lang("Archive for "); ?> <strong><?php single_cat_title(); ?></strong></h2>
       		<?php /* If this is a tag archive */ } elseif( is_tag() ) { ?>
       		<h2><?php fs_lang("Posts Tagged "); ?> <strong><?php single_tag_title(); ?></strong></h2>
       		<?php /* If this is a daily archive */ } elseif (is_day()) { ?>
       		<h2><?php fs_lang("Archive for "); ?> <?php the_time('F jS, Y'); ?></h2>
       		<?php /* If this is a monthly archive */ } elseif (is_month()) { ?>
       		<h2><?php fs_lang("Archive for "); ?> <?php the_time('F, Y'); ?></h2>
       		<?php /* If this is a yearly archive */ } elseif (is_year()) { ?>
       		<h2><?php fs_lang("Archive for "); ?> <?php the_time('Y'); ?></h2>
       		<?php /* If this is an author archive */ } elseif (is_author()) { ?>
       		<h2><?php fs_lang("Author Archive"); ?> </h2>
       		<?php /* If this is a paged archive */ } elseif (isset($_GET['paged']) && !empty($_GET['paged'])) { ?>
       		<h2><?php fs_lang("Blog Archives"); ?></h2>
       		<?php } ?>
       		<ul>
       		<?php while (have_posts()) : the_post(); $count++; ?>
       		<li<?php if ($count==1) echo ' class="first"'; ?>>
       			<a class="attachment" href="<?php the_permalink(); ?>"><?php fs_attachment_image($post->ID, 'thumbnail', 'alt="' . $post->post_title . '"'); ?></a>
       			<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
       			<p><?php echo fs_clean($post->post_excerpt); ?></p>
       		</li>
       		<?php endwhile; ?>
       		</ul>
       		<div class="postnav">
       			<div class="l"><?php next_posts_link('&laquo; '.fs_lang('Older Entries', false)); ?></div>
       			<div class="r"><?php previous_posts_link(fs_lang('Newer Entries', false).' &raquo;'); ?></div>
       		</div>
       	<?php else: ?>
       		<h2><?php fs_lang("Not Found"); ?></h2>
       		<p><?php fs_lang("You are searching for something that isn't here."); ?></p>
       	<?php endif; ?>
       	</div>
       	<!-- END recent posts -->
   
       </div>
       <!-- END content -->
   
       <?php get_sidebar(); get_footer(); ?>
       ```
   
 * Not being a programmer, I cut and pasted as best as I could to follow the KISS
   approach and what I got is what I posted above that you termed as craziness. 
   All I need to know…is how to modify the above code so that it uses a different
   layout for categories 4,5,6,7,8,9,10,453,454,455,456,457,458,459,460. The alternate
   layout for those categories only is:
 *     ```
       <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
       ```
   
 * This is why I am asking for help…how can I do this simply? I don’t know what 
   I’m doing so any help appreciated!!!
 * Thanks so much.
 *  [iridiax](https://wordpress.org/support/users/iridiax/)
 * (@iridiax)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/need-code-help-creating-conditional-archive-template-2/#post-1211552)
 * Your theme is not a simple one to edit. You could always make a copy of archive.
   php, name the copy category-16.php for example (16 is the category number), and
   then delete out the parts you don’t want to show for that category archive. No
   added conditional code is needed.
 *  Thread Starter [wildbug](https://wordpress.org/support/users/wildbug/)
 * (@wildbug)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/need-code-help-creating-conditional-archive-template-2/#post-1211568)
 * Thanks for your solution. That requires making 15 new category php documents 
   but I guess I will use it if no one has a more efficient fix…I’d rather just 
   edit the one template. Anyone else have any ideas? Thank you!!
 *  [thenameisnick](https://wordpress.org/support/users/thenameisnick/)
 * (@thenameisnick)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/need-code-help-creating-conditional-archive-template-2/#post-1211628)
 * Is the archive template you are trying to create supposed to:
 * display posts from all different categories on the same page, just with some 
   posts displayed differently depending on their category?
 * OR
 * display individual category archives with just posts from that specific category,
   only the entire archive is styled differently then another archive of a different
   category?
 * Your question is confusing, the code is not.
 *  Thread Starter [wildbug](https://wordpress.org/support/users/wildbug/)
 * (@wildbug)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/need-code-help-creating-conditional-archive-template-2/#post-1211773)
 * Sorry for the confusion. My intent was to display individual category archives
   with just posts from that specific category, only some categories archives would
   use a different layout. Since I could not figure it out, I came up with a compromise
   for myself, and used the same template for all but just made the attachment photo
   much smaller to condense the lists, which is what my ultimate purpose was.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘need code help creating conditional archive template’ is closed to new
replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 6 replies
 * 3 participants
 * Last reply from: [wildbug](https://wordpress.org/support/users/wildbug/)
 * Last activity: [16 years, 8 months ago](https://wordpress.org/support/topic/need-code-help-creating-conditional-archive-template-2/#post-1211773)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
