-1

In an attempt to use the custom post type, after having registered a brand new CPT I have duplicated both the single and the archive pages appending the "-posttype" bit as specified in the register_post_type function.

However, if I try to load a single post or the archive page, both the pages are not loaded. I can't understand where the problem is.

Here is the code I use to register my Custom Post Type

$labels = array(
    'name' => _x('Facebook', 'post type general name'),
    'singular_name' => _x('Facebook', 'post type singular name'),
    'all_items' => __( 'All posts' ),
    'add_new' => _x('Add new post', 'Facebook'),
    'add_new_item' => __("Add new Facebook post"),
    'edit_item' => __("Edit Facebook post"),
    'new_item' => __("New Facebook post"),
    'view_item' => __("View Facebook posts"),
    'search_items' => __("Search in Facebook posts"),
    'not_found' =>  __('No facebook posts found'),
    'not_found_in_trash' => __('No facebook posts found in trash'),
    'parent_item_colon' => ''
  );
//$post_supports = array('title','editor','comments','thumbnail','excerpt');
$post_supports = array('title','editor','thumbnail');
$args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'query_var' => true,
    //'menu_icon' => get_stylesheet_directory_uri() . '/images/slider.png',
    'rewrite' => true,
    'capability_type' => 'post',
    'hierarchical' => false,
    'menu_position' => null,
    'supports' => $post_supports
  );

Any suggestions to why it is not working?

  • Please edit your question and add the code you've used to register your post type – Pieter Goosen Apr 12 '14 at 9:17
  • possible duplicate of Custom Post Type Template - Archive – Chip Bennett Apr 12 '14 at 15:33
  • "both the pages are not loaded" - meaning you get 404 errors, or the correct content loads but in the wrong templates? when new post types are added, you will get 404 errors using pretty permalinks until you flush the rewrite rules. this can be achieved by visiting the permalinks settings page. – Milo Apr 12 '14 at 15:46
  • @milo I borrowed your comment to improve the upvoted answer below. – Chip Bennett Apr 12 '14 at 18:12
  • Can you 1) ensure that you have placed body_class() and post_class() where appropriate in your Theme, and 2) provide live links so that we can observe the issue? – Chip Bennett Apr 13 '14 at 13:22
3

Try updating permalinks in settings. If that doesn't work please provide the code for your register_posttype function.

both the pages are not loaded

...meaning you get 404 errors, or the correct content loads but in the wrong templates? when new post types are added, you will get 404 errors using pretty permalinks until you flush the rewrite rules. this can be achieved by visiting the permalinks settings page.

  • Not sure what's the connection with the permalinks. Everything else on the site works. Have a look at my code pastebin.com/tp4Zp3Yi – Andrea Moro Apr 12 '14 at 13:31
  • 2
    "Not sure what's the connection with the permalinks." - maybe try the suggestion, and see what happens? – Chip Bennett Apr 12 '14 at 15:31
  • No 404 ... I was getting a page that was not the one expected despite the codex saying that creating a archvie-cpt.php and single-cpt would have result in WP to load them. So the problem is not with the permalink that has been changed in any case, but somewhere else. – Andrea Moro Apr 13 '14 at 10:56
  • "So the problem is not with the permalink that has been changed in any case" - not to beat a dead horse, but if this means that you have tried flushing your rewrite rules, would you mind updating your question with this information? It will help narrow down the cause, and people will stop suggesting it. :) – Chip Bennett Apr 13 '14 at 13:23
  • Apologies ... I just noticed there were a 404 error page. I didn't notice this initially as the I didn't developed the template for that page yet. So I have a changed permalink structure that generate a 404 ... what's the issue? – Andrea Moro Apr 13 '14 at 18:29
1

I have copied your CPT code to my localhost. I had to add register_post_type( 'facebook', $args ); for the CPT to work. Don't know if you forgot to add this piece of code to the code in your question. I also added 'has_archive' => true, as I previously had suggested. I then updated my permalinks, which is set to 'Post name'.

I also created two templates, archive-facebook.php and single-facebook.php. This works as expected.

I tested this in a child theme and in a parent theme.

Other points of note, 'add_new_item' => __("Add new Facebook post"), is wrong for localization. The translator does not recognize a string inside ". You should always always use the single quote ' like 'add_new_item' => __('Add new Facebook post'),

Here is your modified code, and it works perfect as tested

$labels = array(
    'name' => _x('Facebook', 'post type general name'),
    'singular_name' => _x('Facebook', 'post type singular name'),
    'all_items' => __( 'All posts' ),
    'add_new' => _x('Add new post', 'Facebook'),
    'add_new_item' => __('Add new Facebook post'),
    'edit_item' => __('Edit Facebook post'),
    'new_item' => __('New Facebook post'),
    'view_item' => __('View Facebook posts'),
    'search_items' => __('Search in Facebook posts'),
    'not_found' =>  __('No facebook posts found'),
    'not_found_in_trash' => __('No facebook posts found in trash'),
    'parent_item_colon' => ''
  );
//$post_supports = array('title','editor','comments','thumbnail','excerpt');
$post_supports = array('title','editor','thumbnail');
$args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'query_var' => true,
    //'menu_icon' => get_stylesheet_directory_uri() . '/images/slider.png',
    'rewrite' => true,
    'capability_type' => 'post',
    'hierarchical' => false,
    'menu_position' => null,
    'supports' => $post_supports,
    'has_archive' => true
  );

register_post_type( 'facebook', $args );

Please also read the codex in connection to custom post types and register post type

  • That was the first thing I did at the time I registered my post type ... setting it to true. But in any case there is no reason why the single page should not work. – Andrea Moro Apr 12 '14 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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