admin管理员组

文章数量:1201402

I have a custom post type set up (two, actually) that are not displaying properly. I am hoping that it is the same issue with both post types.

Here is my code for one of the post types (portfolio). The second is quite similar.

<?php
add_action('init', 'ccd_portfolio');

function ccd_portfolio() {

    $labels = array(
        'name' => _x('Portfolio', 'post type general name'),
        'singular_name' => _x('Project', 'post type singular name'),
        'add_new' => _x('Add New', 'ccd_portfolio item'),
        'add_new_item' => __('Add New Project'),
        'edit_item' => __('Edit Project'),
        'new_item' => __('New Project'),
        'view_item' => __('View Project'),
        'search_items' => __('Search Projects'),
        'not_found' =>  __('Nothing found'),
        'not_found_in_trash' => __('Nothing found in Trash'),
        'parent_item_colon' => ''
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'menu_icon' => get_stylesheet_directory_uri() . '/images/icons/plugins/portfolio.png',
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => null,
        'supports' => array('title', 'editor', 'excerpt', 'author'),
        'can_export' => true,
        'show_in_menu' => true,
        'has_archive' => true,
        'rewrite' => array('slug' => 'work')
      ); 

    register_post_type( 'portfolio' , $args );
    flush_rewrite_rules();
}

add_action("admin_init", "ccdport_admin_init");

function ccdport_admin_init(){
  add_meta_box("mb_ccdport_details", "Project Details", "ccdport_details", "portfolio", "normal", "high");
  add_meta_box("mb_ccdport_client", "Client", "ccdport_client", "portfolio", "side", "high");
}

add_action( 'admin_enqueue_scripts', 'ccdport_add_datepicker' );
function ccdport_add_datepicker(){
  wp_enqueue_script( 'jquery-ui-datepicker' );
  wp_enqueue_style('jquery-datepicker-style', '.8.2/themes/smoothness/jquery-ui.css');
}
function ccdport_details(){
  global $post;
  $custom = get_post_custom($post->ID);
  $preview_url = $custom['preview_url'][0];
  $start_date = $custom['start_date'][0];
  $end_date = $custom['end_date'][0];
?>
  <p><label>Preview URL: <strong>http://</strong>
    <input type="text" size="35" name="preview_url" value="<?php echo $preview_url; ?>" /></p>
  <p><label>Start date:</label>
    <input id="startDate" type="text" size="15" class="DatePicker" name="start_date" value="<?php echo $start_date; ?>" /> &nbsp;
    <label>End date:</label>
    <input id="endDate" type="text" size="15" class="DatePicker" name="end_date" value="<?php echo $end_date; ?>" /></p>
    <script>
      jQuery(document).ready(function() {
        jQuery('input.DatePicker').datepicker({
          dateFormat : 'dd-mm-yyyy'
        });
      });
    </script>
<?php
}

function ccdport_client(){
  global $post;
  $custom = get_post_custom($post->ID);
  $client = $custom['project_client'][0];
  $name = $custom['client_name'][0];
  $args = array(
    'post_type' => 'clients',
    'posts_per_page' => -1,
    'orderby' => 'name',
    'order' => 'ASC'
  );
?>
  <p><label>Client name</label></p>
  <input type="text" name="client_name" value="<?php echo $name; ?>" />
  <p><label>Organisation</label></p>
<?php
  $query = new WP_Query( $args );
  if ( $query->have_posts() ){
    echo '<select name="project_client">';
    while ( $query->have_posts() ){
      $query->the_post();
      $slug = $post->post_name;
      echo '<option value="'.$slug.'" '.selected($slug, $client).'>'.get_the_title().' ('.$slug.')</option>';
    }
    echo '</select>';
  }
  else { echo '<p>There are currently no clients registered</p>'; }
}

add_action('save_post', 'ccdport_save_details');

function ccdport_save_details(){
  global $post;

  update_post_meta($post->ID, "client_name", $_POST["client_name"]);
  update_post_meta($post->ID, "project_client", $_POST["project_client"]);
  update_post_meta($post->ID, "portfolio", $_POST["portfolio"]);
  update_post_meta($post->ID, "preview_url", $_POST["preview_url"]);
}
?>

Each post type is included in a separate file, and called to functions.php with an include command. These are showing up in the admin area perfectly, posts are being saved perfectly, etc. However, the theme files (archive-portfolio.php and single-portfolio.php) are not loading. Instead, my custom home page is being displayed.

I don't know what the issue would be. I have flushed the rewrite rules with each post type, and removing that does nothing either. I have only recently started with custom post types, so any assistance would be extremely helpful. I have had a look through the other answers and they don't seem to be helpful in my situation. I've tried resaving my permalinks, to no avail. I've tried changing 'has_archive' => true to 'has_archive' => 'portfolio', nothing. Is there something I have missed? More than likely something glaringly obvious?

I have a custom post type set up (two, actually) that are not displaying properly. I am hoping that it is the same issue with both post types.

Here is my code for one of the post types (portfolio). The second is quite similar.

<?php
add_action('init', 'ccd_portfolio');

function ccd_portfolio() {

    $labels = array(
        'name' => _x('Portfolio', 'post type general name'),
        'singular_name' => _x('Project', 'post type singular name'),
        'add_new' => _x('Add New', 'ccd_portfolio item'),
        'add_new_item' => __('Add New Project'),
        'edit_item' => __('Edit Project'),
        'new_item' => __('New Project'),
        'view_item' => __('View Project'),
        'search_items' => __('Search Projects'),
        'not_found' =>  __('Nothing found'),
        'not_found_in_trash' => __('Nothing found in Trash'),
        'parent_item_colon' => ''
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'menu_icon' => get_stylesheet_directory_uri() . '/images/icons/plugins/portfolio.png',
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => null,
        'supports' => array('title', 'editor', 'excerpt', 'author'),
        'can_export' => true,
        'show_in_menu' => true,
        'has_archive' => true,
        'rewrite' => array('slug' => 'work')
      ); 

    register_post_type( 'portfolio' , $args );
    flush_rewrite_rules();
}

add_action("admin_init", "ccdport_admin_init");

function ccdport_admin_init(){
  add_meta_box("mb_ccdport_details", "Project Details", "ccdport_details", "portfolio", "normal", "high");
  add_meta_box("mb_ccdport_client", "Client", "ccdport_client", "portfolio", "side", "high");
}

add_action( 'admin_enqueue_scripts', 'ccdport_add_datepicker' );
function ccdport_add_datepicker(){
  wp_enqueue_script( 'jquery-ui-datepicker' );
  wp_enqueue_style('jquery-datepicker-style', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/smoothness/jquery-ui.css');
}
function ccdport_details(){
  global $post;
  $custom = get_post_custom($post->ID);
  $preview_url = $custom['preview_url'][0];
  $start_date = $custom['start_date'][0];
  $end_date = $custom['end_date'][0];
?>
  <p><label>Preview URL: <strong>http://</strong>
    <input type="text" size="35" name="preview_url" value="<?php echo $preview_url; ?>" /></p>
  <p><label>Start date:</label>
    <input id="startDate" type="text" size="15" class="DatePicker" name="start_date" value="<?php echo $start_date; ?>" /> &nbsp;
    <label>End date:</label>
    <input id="endDate" type="text" size="15" class="DatePicker" name="end_date" value="<?php echo $end_date; ?>" /></p>
    <script>
      jQuery(document).ready(function() {
        jQuery('input.DatePicker').datepicker({
          dateFormat : 'dd-mm-yyyy'
        });
      });
    </script>
<?php
}

function ccdport_client(){
  global $post;
  $custom = get_post_custom($post->ID);
  $client = $custom['project_client'][0];
  $name = $custom['client_name'][0];
  $args = array(
    'post_type' => 'clients',
    'posts_per_page' => -1,
    'orderby' => 'name',
    'order' => 'ASC'
  );
?>
  <p><label>Client name</label></p>
  <input type="text" name="client_name" value="<?php echo $name; ?>" />
  <p><label>Organisation</label></p>
<?php
  $query = new WP_Query( $args );
  if ( $query->have_posts() ){
    echo '<select name="project_client">';
    while ( $query->have_posts() ){
      $query->the_post();
      $slug = $post->post_name;
      echo '<option value="'.$slug.'" '.selected($slug, $client).'>'.get_the_title().' ('.$slug.')</option>';
    }
    echo '</select>';
  }
  else { echo '<p>There are currently no clients registered</p>'; }
}

add_action('save_post', 'ccdport_save_details');

function ccdport_save_details(){
  global $post;

  update_post_meta($post->ID, "client_name", $_POST["client_name"]);
  update_post_meta($post->ID, "project_client", $_POST["project_client"]);
  update_post_meta($post->ID, "portfolio", $_POST["portfolio"]);
  update_post_meta($post->ID, "preview_url", $_POST["preview_url"]);
}
?>

Each post type is included in a separate file, and called to functions.php with an include command. These are showing up in the admin area perfectly, posts are being saved perfectly, etc. However, the theme files (archive-portfolio.php and single-portfolio.php) are not loading. Instead, my custom home page is being displayed.

I don't know what the issue would be. I have flushed the rewrite rules with each post type, and removing that does nothing either. I have only recently started with custom post types, so any assistance would be extremely helpful. I have had a look through the other answers and they don't seem to be helpful in my situation. I've tried resaving my permalinks, to no avail. I've tried changing 'has_archive' => true to 'has_archive' => 'portfolio', nothing. Is there something I have missed? More than likely something glaringly obvious?

Share Improve this question edited Sep 17, 2016 at 2:54 Ethan Rævan 4,0295 gold badges27 silver badges55 bronze badges asked Jul 16, 2014 at 19:53 NateNate 2291 gold badge5 silver badges16 bronze badges 7
  • Have you created a portfolio page with the following path: http://domain.com/portfolio? If so, try deleting it and then visiting that path. – deflime Commented Jul 16, 2014 at 20:46
  • No, I haven't. There is another post type that this is not working with (as mentioned earlier) which did have a page set up with the same slug (products). However, I renamed the page in Wordpress and that page is fine, but the corresponding custom post type is not. – Nate Commented Jul 16, 2014 at 20:49
  • I meant to put http://domain.com/work since you have a rewrite rule. I'm guessing same answer applies? So it's using your index.php file to render the page? – deflime Commented Jul 16, 2014 at 21:03
  • Yes, the same answer applies (I gathered you meant /work/ anyway), and no, it's a custom home page - page-home.php. I have a static home page set up which uses a custom template, and index.php handles my blog page. Sorry for not stating that, I didn't think it would have made a difference. I have a single.php and an archives.php page, neither of which are being loaded; it seems to be bypassing them all. Something tells me it may be a permalink issue somewhere, as /products/ throws up a 404 error, but /work/ gives my home page, and as I said, that uses a custom template. – Nate Commented Jul 16, 2014 at 21:07
  • However, I don't know for certain and have no idea where I would be looking or even if I am on the right lines, so thought best to ask for advice. – Nate Commented Jul 16, 2014 at 21:08
 |  Show 2 more comments

2 Answers 2

Reset to default 1

As per the OP's answer that was added in their question, I've separated it instead. In short:

...deactivating and reactivating plugins, commenting and uncommenting, I now have my site working as intended.

Here's the elaboration given by the OP as well:

I have a couple of custom post types in my functions.php file. Each one is in its own .php file to make them easier for me to manage and included in functions.php. However, I had a few issues with them, which was resolved. I had a space at the top of the page when I included more than one .php file. That was solved a short while ago, but as a temporary fix, I encased the pages in a if ( is_admin() ) function. I wasn't aware that would also affect the template pages as well.

I had a similar issue - added a new Custom Post Type whose slug was an old page. After permanently deleting the old page my custom posts and its archive wouldn't show up, instead I got the theme home page.

The solution was to go to Settings > Permalinks, change it to something else temporarily, press save, change it back to the original setting, press save again. Now the Custom Post Type is working.

本文标签: homepageCustom post type is showing custom home pagenot archive page