Pergunta

I have registered two custom post types using the following code

add_action( 'init', 'create_post_types' );
function create_post_types() {

    register_post_type( 'sector', array(
        'labels' => array(
            'name' => __( 'Sectors' ),
            'singular_name' => __( 'Sector' )
        ),
        'public' => true,
        'has_archive' => true,
        'supports' => array( 'title', 'editor' )
    ));

    register_post_type( 'player', array(
        'labels' => array(
            'name' => __( 'Players' ),
            'singular_name' => __( 'player' )
        ),
        'public' => true,
        'has_archive' => false,
        'supports' => array( 'title', 'editor' )
    ));
}

This correctly creates the post types in the Wordpress admin & I am able to create new posts. So far so good. The problem comes when trying to view a single post or list these posts within an archive.

In my attempts I have created archive-sector.php and single-sector.php... following the documentation I would expect the code in the 'single' file to display when I visit a URL such as this: http://mydomain.com/sector/a-single-post/

But it doesn't it simply renders the code from the index.php. Which is rather frustrating. Can anyone point out where I am going wrong.

What confuses me further is I am able to reference the posts within a custom WP_Query within my front-page.php:

<?php
    $args=array(
        'post_type' => 'sector',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'caller_get_posts'=> 1
    );
    $get_sectors = new WP_Query($args);
    if( $get_sectors->have_posts() ) {
        while ($get_sectors->have_posts()) : $get_sectors->the_post();
?>
    <div class="medium-6 columns">
        <div class="panel">
            <h3>
                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            </h3>
        </div>
    </div>
<?php
    endwhile;
    }
    wp_reset_query();
?>

This displays the post titles no problem!

Any suggestions would be great as I'm at a loss with this one!

Foi útil?

Solução

I encountered the same problem before and the solution was to flush the rewrite rules.

To do so, go to Settings > Permalinks and just save your current settings.

This is stated on the Wordpress documentation here: http://codex.wordpress.org/Post_Types#Custom_Post_Type_Templates

Outras dicas

I've found the solution (for me at least)... The code above seems to work fine. All that was needed was to re-save the settings for permalinks (Settings > Permalinks) within Wordpress itself.

I can only presume this needed to be done to clear the cache as the /%category%/%postname%/ reference had been inserted prior to registering the custom post types.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top