I’ve created a custom post type, and have successfully added a few entries. I can call these entries out with query_posts()
to show on the front page, but the_permalink()
on each of them just sends me to a “Page not found” 404.
Am I missing something? I’m currently running on http://localhost
, so the results of the_permalink()
from the front page custom post type loop sends the user to http://localhost/PU/PU2010/website/cartoons/einstein-on-california
.
functions.php
function createCartoonPostType() {
register_post_type( 'cartoon', array(
'label' => 'Cartoon',
'public' => true,
'hierarchical' => true,
'supports' => array( 'title', 'editor', 'thumbnail', 'comments' ),
'rewrite' => array( 'slug' => 'cartoons' )
) );
}
add_action( 'init', 'createCartoonPostType' );
According to this, I should be able to just create single-cartoon.php
, correct?
single-cartoon.php
<?php get_header(); ?>
<div class="container_20">
<div class="grid_14">
<div class="bodybox">
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
</div>
</div>
<div class="grid_6">
<?php get_template_part( 'social', 'box' ); ?>
<?php get_sidebar(); ?>
</div>
<div class="clear"></div>
</div>
<?php get_footer(); ?>
loops-cartoons.php (frontpage loop)
<?php query_posts( 'post_type=cartoon&posts_per_page=1' ); ?>
<div class="cartoons-box">
<ul class="cartoons-list">
<?php if ( ! have_posts() ) : ?>
Sorry, no posts.
<?php else : while ( have_posts() ) : the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>" class="preview-image">
<?php the_post_thumbnail( 'featured' ); ?>
</a>
</li>
<?php endwhile; endif; ?>
<div class="clear"></div>
</ul>
</div>
<?php wp_reset_query(); ?>
Did you go to Admin -> Settings -> Permalinks after setting up the post type? The permalink structure hasn’t been added until you’ve done that. It could be the cause of your problem.
The permalinks page fires
$wp_rewrite->flush_rules();
each time the page is visited, so it’s not even necessary to save.After registering the custom post types you need to rebuild permalinks. You can do that manually by visiting Admin -> Settings -> Permalinks (as John commented), you can rebuild permalinks within your code by calling
$wp_rewrite->flush_rules();
or if you’re lazy you can make use of the Permafrost (WordPress Plugin).I had the same problem – and every time I added a new page it required the permalinks structure reset. Adding this:
within your createCartoonPostType() function immediately after the register_post_type part solves the problem though and means you don’t need to use the keep resetting permalinks.