1

I have registered a custom post type "tutorials" with the following snippet.


<?php
/*
Template Name: Tutorial Archive
*/
get_header(); ?>
<div id="content">
<div class="archive-page">
<?php
        $temp = $wp_query;
        $wp_query = null;
        $wp_query = new WP_Query();
        $wp_query->query('showposts=8&post_type=tutorials'.'&paged='.$paged);
        if ($wp_query->have_posts()) :
?>
<h1>Tutorials Archive</h1>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<article class="posts" id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<div class="posts-meta"><ul><li class="post-auth">by Admin</li><li class="post-date"><?php the_time('F jS, Y') ?></li><li class="post-view"><?php echo get_post_views(get_the_ID()); ?></li><li class="post-comment"><?php comments_number( '0 Comment', '1 Comment', '% Comments' ); ?></li></ul></div>
<div class="posts-entry"><?php $excerpt = get_the_excerpt(); echo archive_excerpt($excerpt,40); ?> &rarr;</div></article> <?php endwhile; ?> <?php if( function_exists('numeric_pagination')) { numeric_pagination(); } else { ?> <div class="navigation"> <div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div> <div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div> </div><?php } ?>
<?php $wp_query = null; $wp_query = $temp; // reset ?> <?php else : ?> <div class="archive-page"> <h1>Not Found!</h1> <p>Sorry, but you are looking for something that isn't here.</p> <?php endif; ?> </div><!-- $ archive ends --> </div><!-- # content ends --> <?php get_sidebar(); ?> <?php get_footer(); ?> ----------- this is imy functions.php file ------------- <?php // numeric_pagination function on functions.php function numeric_pagination() { if( is_singular() ) return; global $wp_query; if( $wp_query->max_num_pages <= 1 ) return; $paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1; $max = intval( $wp_query->max_num_pages ); if ( $paged >= 1 ) $links[] = $paged; if ( $paged >= 3 ) { $links[] = $paged - 1; $links[] = $paged - 2; } if ( ( $paged 2 ) <= $max ) { $links[] = $paged 2; $links[] = $paged 1; } echo '<div class="navigation"><ul>' . "\n"; if ( get_previous_posts_link() ) printf( '<li>%s</li>' . "\n", get_previous_posts_link() ); if ( ! in_array( 1, $links ) ) { $class = 1 == $paged ? ' class="active"' : ''; printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' ); if ( ! in_array( 2, $links ) ) echo '<li>%u2026</li>'; } sort( $links ); foreach ( (array) $links as $link ) { $class = $paged == $link ? ' class="active"' : ''; printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link ); } if ( ! in_array( $max, $links ) ) { if ( ! in_array( $max - 1, $links ) ) echo '<li>%u2026</li>' . "\n"; $class = $paged == $max ? ' class="active"' : ''; printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max ); } if ( get_next_posts_link() ) printf( '<li>%s</li>' . "\n", get_next_posts_link() ); echo '</ul></div>' . "\n"; }

// registering custom post type (tutorials) add_action( 'init', 'wpb_tutorials' ); function wpb_tutorials() { register_post_type( 'tutorials', array( 'labels' => array( 'name' => 'Tutorials', 'singular_name' => 'Tutorial', 'add_new' => 'Add Tutorial', 'add_new_item' => 'New Tutorial', 'edit_item' => 'Edit Tutorial', 'new_item' => 'New Tutorial', 'view_item' => 'View Tutorial', 'all_items' => 'All Tutorials', 'search_items' => 'Search Tutorial', 'not_found' => 'No Tutorial Found', 'not_found_in_trash' => 'No Tutorial in the trash', 'parent_item_colon' => '', 'menu_name' => 'Tutorials'), 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'exclude_from_search' => false, 'query_var' => true, 'capability_type' => 'post', 'rewrite' => array('slug' => 'tutorials/%tcat%'), 'can_export' => true, 'has_archive' => true, 'hierarchical' => false, 'show_in_nav_menus' => true, 'menu_position' => 50, 'taxonomies' => array('post_tag'), 'menu_icon' => get_template_directory_uri() . '/images/icons/icon-tutorial.png', 'supports' => array( 'title', 'editor', 'tags', 'comments' ), ) ); } // register taxonomy register_taxonomy('tcat', array('tutorials'), array( 'labels' => array( 'name' => 'Tutorial Category', 'singular_name' => 'Tutorial Category', 'search_items' => 'Search Tutorial Categories', 'all_items' => 'All Tutorial Categories',
'add_new_item' => 'Add Tutorial Category', 'edit_item' => 'Edit Tutorial Category', 'update_item' => 'Update Tutorial Category', 'new_item_name' => 'New Tutorial Category', 'parent_item' => 'Parent Tutorial Category', 'parent_item_colon' => 'Parent Tutorial Category:', 'not_found' => 'No Tutorial Category Found', 'not_found_in_trash' => 'No Tutorial Category Found in Trash', 'menu_name' => 'Categories'), 'query_var' => true, 'public' => true, 'show_admin_column' => true, 'sort' => '', 'hierarchical' => true, 'rewrite' => array( 'slug' => 'tutorials'), 'show_ui' => true) ); // rewrite permalink - category based function build_tutorial_permalink($link, $post) { if ($post->post_type != 'tutorials') return $link; if ($cats = get_the_terms($post->ID, 'tcat')) $link = str_replace('%tcat%', array_pop($cats)->slug, $link); return $link; } add_filter('post_type_link', 'build_tutorial_permalink', 10, 2); ?>

Seems like, its working just fine without any major issues. However, what's really bothering me is I couldn't get the archive page working from: wpbloggerz.com/tutorials/. I already have archive-tutorials.php file (copied from archive.php). Yes, I tried with changing the permalink structure but it didn't work. So what I did is, I created a page with "tutorials" title (with custom page template) and the permalink of that page is also wpbloggerz.com/tutorials/ (since the archive page wasn't working). It has custom pagination function which I have also included on pastebin url. The pagination is not working (gives me 404 error) even though it shows the right url structure but the same works on other pages.

I have taxonomy-tcat.php to display posts archived with custom taxonomies which is working fine without any issues.

Now, only couple of days back I came across http://codex.wordpress.org/Post_Types page and read about "URLs of Namespaced Custom Post Types Identifiers" section. The example on that page shows "acme_product" is being registered as custom post type and using the "products" slug to rewrite the permalink structure. Do you think I should have registered my custom post type as "mytutorials" (or something else) instead of "tutorials"? or something else went wrong? I am so confused. Please help. Thanks.

2 Answers 2

1

You can post code directly into your question. You should edit the question to add your code so we don't have to go looking for it. Plus if you ever delete that pastebin, this question is worthless to future users.

You have registered the post type to have an archive, and then your template is repeating the query for tutorials. That isn't very efficient. I would delete the page and the page template entirely. archive-tutorials.php will work and doesn't need an additional WP_Query().

I'd probably remove the rewrite parameter from your post type

 'rewrite' => array('slug' => 'tutorials/%tcat%'), 

and see if that makes a difference. Be sure to re-save your permalinks after you do. If it does then you have a different question about permalinks.

3
  • I did as you mentioned and it worked right away. However, my single-tutorials.php page stopped working and I can't see the posts now. Commented Aug 2, 2013 at 3:43
  • Nevermind, I got it working simply changing 'has_archive' => true, parameter to 'has_archive' => tutorials,. Thanks anyway. Commented Aug 2, 2013 at 4:38
  • 4
    If this is the answer you should accept it, or if not, then post your own answer and accept that. cheers! Commented Aug 2, 2013 at 10:23
1

Their archives will use archive-{post_type}.php

'has_archive' => true,

has_archive (boolean or string) (optional) Enables post type archives. Will use $post_type as archive slug by default.

Default: false

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