Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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(); ?>
share|improve this question
4  
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. –  John P Bloch Nov 12 '10 at 0:01
    
Weird...just have to open up that page and it fixed everything. @John please post this as the answer so I can give you credit. –  Jonathan Sampson Nov 12 '10 at 0:04

3 Answers 3

up vote 4 down vote accepted

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.

share|improve this answer

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).

share|improve this answer

I had the same problem - and every time I added a new page it required the permalinks structure reset. Adding this:

flush_rewrite_rules();

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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