2

I have registered a custom post type and I want to create a page where I will list some posts, it will be like an index page.

I have created an archive page called mycpt-archive.php and a page called myCPT.php.

I've added a custom URL in the menu called "myCPT" like this : www.mywebsite.com/mycpt Now, when I click from the front-end menu on "myCPT" it displays the mycpt-archive.php content and not the myCPT.php.

I've looked through the template hierarchy from CODEX and it seems I'm on the right track.

When I click on "myCPT" from the front-end menu the page displayed is mycpt-archive.php not myCPT.php which I'm expecting to open.

What am I missing here ? Thank you !

3
  • 1
    It's not clear why are you creating two templates for seemingly one task? Also according to hierarchy template name would be in archive-$posttype.php format, so I am not sure how is your reversed variant loads at all.
    – Rarst
    Commented Dec 19, 2013 at 16:29
  • I need two pages, one for listing some posts from the custom post type which should be the default one. and another one for listing an archive of all the posts within that custom post type which will be the archive-myCPT.php
    – agis
    Commented Dec 19, 2013 at 17:48
  • The idea is to have a page template which will be used to display some posts of within the custom post type but not to be displayed in the archive-myCPT.php or is not displayed by default if it's a template ?
    – agis
    Commented Dec 19, 2013 at 18:13

2 Answers 2

3

If you have:

register_post_type( 'my_custom_post_type', $args );

And you need a custom page to displays all entries from this custom post type, you have to create: archive-my_custom_post_type.php. But if you don't need a custom page, wordpress will use archive.php to display you custom post type archive.

If you only need to customize the entry page, you have to create: single-my_custom_post_type.php

Where are you creating the custom post type, in theme functions.php or using a plugin?

If using functions.php, you need to create archive-my_custom_post_type.php or single-my_custom_post_type.php in theme's folder.

If using a plugin, you need to create archive-my_custom_post_type.php or single-my_custom_post_type.php in plugins's folder and point wordpress to read it, so include this function in your plugin:

function get_custom_post_type_template($template) {
    global $post;

    if ($post->post_type == 'my_custom_post_type') {
        $template = dirname( __FILE__ ) . '/archive-my_custom_post_type.php';
    }
    return $template;
}

//add_filter( "single_template", "get_custom_post_type_template" ); //for single page
add_filter( "archive_template", "get_custom_post_type_template" ); //for archive
1
  • I'm not using a plugin, I want just to create a page template where I can show some posts from my CPT and another one called "archive-myCPT.php" and there to display the archives.
    – agis
    Commented Dec 19, 2013 at 18:23
0

It almost sounds like you would create a custom template page named whatever and put the custom query code you want into that particular template page. Once you make the template page, create an actual wordpress page and set it to use that custom template page. Does that make sense.

  1. Create custom template page.
  2. put the necessary comments to name the template page.
  3. Write the custom wp_query to pull the particular posts you want
  4. Create a page inside of wordpress.
  5. Set it to use the template page you create.
  6. Hit publish

Once you are done, you will probably want to edit the template page to tweak and refine your query.

Let me know if I'm on the right track for your needs.

2
  • I've created a template file, but I can not assign it from the dashboard, is working just for the normal pages not for the custom post type pages.
    – agis
    Commented Dec 20, 2013 at 13:13
  • Sorry for the delay, in response. You will need to create a custom wp_query() inside the template with its own loop. You could do something like $postTypeQuery = new wp_query( array('post_type'=> 'postTypeSlug')); if($postTypeQuery->have_posts()){ while($postTypeQuery->have_post(){ $postTypeQuery->the_post(); echo get_the_title(); echo get_the_id(); echo get_the_content(); } } Commented Jan 13, 2014 at 23:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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