Create Your own Archive Page for Custom Post types and Custom Taxonomies

In this post i will tell you how you can create your own archive page for Custom post types and custom taxonomies.I had already discussed about how you can create your own custom post types and custom taxonomies.
Create Your own Archive Page for Custom Post types and Custom Taxonomies

Create Your Own Archive for Custom Post Type

Step 1: Create a page archive-{post-type}.php for your custom post type archive in your WordPress Theme folder.
For Example: I have my own custom post types called Deals and Affreview so wordpress would look for archive-deals.php and archive-affreview.php
Step 2: Your archive-{post-type}.php should look like:

<?php
/*
Template Name: Custom Post type Archive
Description: MyCodingTricks tutorial for creating own page for custom post type
*/
?>
<?php get_header(); ?>
 
    <div id="main-content" class="main-content">
 
    <div id="primary" class="content-area">
        <div id="content" class="site-content" role="main">
 
            <header class="archive-header">
                <h1 class="archive-title">
                    <?php post_type_archive_title(); ?>
                </h1>
            </header><!-- .archive-header -->
             
                         
        </div><!-- #content -->
    </div><!-- #primary -->
</div><!-- #main-content -->
 
<?php
get_sidebar();
get_footer();
?>

If you wants to get custom field value then call the get_post_meta() function.
For example i have to get the deal excerpt meta value for my custom post type deals,so i will call it like:

<?php echo esc_html(get_post_meta( get_the_ID(), 'deal_excerpt', true )); ?>

Step 3: Create a single-{post-type}.php file in your plugin or Template folder.
and Your single-{post-type}.php look like:

<?php
/*
Template Name: Custom Post Type Single
Description: MyCodingTricks tutorial for creating single page for custom post types
*/
?>
<?php get_header(); ?>

<section class="content">
	<div class="pad group">
<?php
 if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
			<article <?php post_class(); ?>>	
				<div class="post-inner group">
					
					<a  href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><h1 class="post-title"><?php the_title(); ?></h1></a>
					<p class="post-byline">By <?php the_author_posts_link(); ?> &middot; <spanclass="post_date date updated"><?php the_time(get_option('date_format')); ?></span></p>
					
					<div class="clear"></div>
					
					<div class="entry">	
						<div class="entry-inner" itemprop="articleBody">
							<a href='<?php the_permalink(); ?>' target='_blank'><?php the_post_thumbnail( array( 250, 250 ) ); ?></a>
							<?php the_content(); ?>
						</div>
						<div class="clear"></div>				
					</div><!--/.entry-->
					
				</div><!--/.post-inner-->	
			</article><!--/.post-->				
		<?php endwhile; else : ?>


 	<!-- The very first "if" tested to see if there were any Posts to -->
 	<!-- display.  This "else" part tells what do if there weren't any. -->
 	<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>


 	<!-- REALLY stop The Loop. -->
 <?php endif; ?>
		
		<div class="clear"></div>
		
		<?php the_tags('<p class="post-tags"><span>Tags:</span> ','','</p>'); ?>
		
	</div><!--/.pad-->
	
</section><!--/.content-->

<?php get_sidebar(); ?>

<?php get_footer(); ?>

Create Your Own Page for Your Custom Taxonomies

Step 1: Create a taxonomy-{taxonomy}.php for your Taxonomy.
Step 2: Your taxonomy-{taxonomy}.php page should look like:

<?php
/**
 * Template name: {taxonomy} Archive 
 */
get_header();
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
?>
<div class="wrapper">
  <div class="primary-content">
    <h1 class="archive-title"><?php echo apply_filters( 'the_title', $term->name ); ?> News</h1>

    <?php if ( !empty( $term->description ) ): ?>
    <div class="archive-description">
      <?php echo esc_html($term->description); ?>
    </div>
    <?php endif; ?>

    <?php if ( have_posts() ): while ( have_posts() ): the_post(); ?>

    <div id="post-<?php the_ID(); ?>" <?php post_class('post clearfix'); ?>>
      <h2 class="post-title"><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
      <div class="content clearfix">
        <div class="post-info">
          <p><?php the_time(get_option('date_format')); ?> by <?php the_author_posts_link(); ?></p>
        </div><!--// end .post-info -->
        <div class="entry">
          <?php the_content( __('Full story…') ); ?>
        </div>
      </div>
    </div><!--// end #post-XX -->

    <?php endwhile; ?>

    <div class="navigation clearfix">
      <div class="alignleft"><?php next_posts_link('« Previous Entries') ?></div>
      <div class="alignright"><?php previous_posts_link('Next Entries »') ?></div>
    </div>

    <?php else: ?>

    <h2 class="post-title">Nohing in <?php echo apply_filters( 'the_title', $term->name ); ?></h2>
    <div class="content clearfix">
      <div class="entry">
        <p>It seems there isn't anything happening in <strong><?php echo apply_filters( 'the_title', $term->name ); ?></strong> right now. Check back later, something is bound to happen soon.</p>
      </div>
    </div>

    <?php endif; ?>
  </div><!--// end .primary-content -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

Create Custom Archive Page for Child Taxonomy Terms

You can create custom pages for your child taxonomy terms.

  • taxonomy-{taxonomy}.php for your Taxonomy.
  • taxonomy-{taxonomy}-{slug}.php for your taxonomy terms.

Summary

As i had already described above that how you can create custom archive page for your taxonomy and custom post types.I had also described that how you can create single post template for your custom post types.
Here are some useful links which you can use in creating your template.

[moptin id="3098"]

Shubham Kumar

Hey, I am Shubham and i love Blogging, Coding and exploring new things and obviously sharing my experience with you.

Leave a Reply

Your email address will not be published. Required fields are marked *

shares