• Resolved tiaanswart

    (@tiaanswart)


    Ok here goes…

    I’m new to php and wordpress been working on it for about 4 months and taught myself quite a bit…

    I am trying to develop a new theme so far I have done everything correctly without errors…

    However i am now trying to create a new custom post type names portfolio using the tutorial on the below page:

    https://teamtreehouse.com/blog/create-your-first-wordpress-custom-post-type

    i did everything as explained in the tutorial and added 2 extra fields “Image Url” and “Month” my final code looks like this in my dev site:

    /*******************************
     Custom Post Type - Portfolio
    ********************************/
    
    add_action('init', 'portfolio_register');
    
    function portfolio_register() {
    
    	$labels = array(
    		'name' => _x('My Portfolio', 'post type general name'),
    		'singular_name' => _x('Portfolio Item', 'post type singular name'),
    		'add_new' => _x('Add New', 'portfolio item'),
    		'add_new_item' => __('Add New Portfolio Item'),
    		'edit_item' => __('Edit Portfolio Item'),
    		'new_item' => __('New Portfolio Item'),
    		'view_item' => __('View Portfolio Item'),
    		'search_items' => __('Search Portfolio'),
    		'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/portfolio/portfolio.png',
    		'rewrite' => true,
    		'capability_type' => 'post',
    		'hierarchical' => false,
    		'menu_position' => null,
    		'supports' => array('title','editor','thumbnail')
    	  ); 
    
    	register_post_type( 'portfolio' , $args );
    
        flush_rewrite_rules();
    }
    
    register_taxonomy("Skills", array("portfolio"), array("hierarchical" => true, "label" => "Skills", "singular_label" => "Skill", "rewrite" => true));
    
    add_action("admin_init", "admin_init");
    
    function admin_init(){
      add_meta_box("year_completed-meta", "Year Completed", "year_completed", "portfolio", "side", "low");
      add_meta_box("month_completed-meta", "Month Completed", "month_completed", "portfolio", "side", "low");
      add_meta_box("image_url-meta", "Image Url", "image_url", "portfolio", "normal", "low");
      add_meta_box("credits_meta", "Design & Build Credits", "credits_meta", "portfolio", "normal", "low");
    }
    
    function year_completed(){
      global $post;
      $custom = get_post_custom($post->ID);
      $year_completed = $custom["year_completed"][0];
      ?>
      <label>Year:</label>
      <input name="year_completed" value="<?php echo $year_completed; ?>" />
      <?php
    }
    
    function month_completed(){
      global $post;
      $custom = get_post_custom($post->ID);
      $month_completed = $custom["month_completed"][0];
      ?>
      <label>Month:</label>
      <input name="month_completed" value="<?php echo $month_completed; ?>" />
      <?php
    }
    
    function image_url(){
      global $post;
      $custom = get_post_custom($post->ID);
      $image_url = $custom["image_url"][0];
      ?>
      <p><label>Image Url:</label><br />
      <textarea cols="150" rows="1" name="image_url"><?php echo $image_url; ?></textarea></p>
      <?php
    }
    
    function credits_meta() {
      global $post;
      $custom = get_post_custom($post->ID);
      $designers = $custom["designers"][0];
      $developers = $custom["developers"][0];
      $producers = $custom["producers"][0];
      ?>
      <p><label>Designed By:</label><br />
      <textarea cols="150" rows="2" name="designers"><?php echo $designers; ?></textarea></p>
      <p><label>Built By:</label><br />
      <textarea cols="150" rows="2" name="developers"><?php echo $developers; ?></textarea></p>
      <p><label>Produced By:</label><br />
      <textarea cols="150" rows="2" name="producers"><?php echo $producers; ?></textarea></p>
      <?php
    }
    
    add_action('save_post', 'save_details');
    
    function save_details(){
      global $post;
    
      update_post_meta($post->ID, "year_completed", $_POST["year_completed"]);
      update_post_meta($post->ID, "month_completed", $_POST["month_completed"]);
      update_post_meta($post->ID, "image_url", $_POST["image_url"]);
      update_post_meta($post->ID, "designers", $_POST["designers"]);
      update_post_meta($post->ID, "developers", $_POST["developers"]);
      update_post_meta($post->ID, "producers", $_POST["producers"]);
    }
    
    add_action("manage_posts_custom_column",  "portfolio_custom_columns");
    add_filter("manage_edit-portfolio_columns", "portfolio_edit_columns");
    
    function portfolio_edit_columns($columns){
      $columns = array(
        "cb" => "<input type='checkbox' />",
        "title" => "Portfolio Title",
        "description" => "Description",
        "year" => "Year Completed",
        "month" => "Month Completed",
        "skills" => "Skills",
      );
    
      return $columns;
    }
    
    function portfolio_custom_columns($column){
      global $post;
    
      switch ($column) {
        case "description":
          the_excerpt();
          break;
        case "year":
          $custom = get_post_custom();
          echo $custom["year_completed"][0];
          break;
        case "month":
          $custom = get_post_custom();
          echo $custom["month_completed"][0];
          break;
        case "skills":
          echo get_the_term_list($post->ID, 'Skills', '', ', ','');
          break;
      }
    }

    When creating a new portfolio item, the portfolio item is not displayed when visiting the permalink…

    I then enabled the wp debug and got the below error:

    Notice: wp_enqueue_script was called incorrectly. Scripts and styles should not be registered or enqueued until the wp_enqueue_scripts, admin_enqueue_scripts, or init hooks. Please see Debugging in WordPress for more information. (This message was added in version 3.3.) in C:\Sites\site\wp-includes\functions.php on line 2825

    On the same subject my breadcrums is also not working now…

    I get the below error for breadcrumbs:

    Notice: Undefined offset: 0 in C:\Sites\site\wp-content\themes\theme\functions.php on line 834 Catchable fatal error: Object of class WP_Error could not be converted to string in C:\Sites\site\wp-content\themes\theme\functions.php on line 835

    The functions.php breadcrumbs code is refered to is:

    } elseif ( is_single() ) {
          $cat = get_the_category(); $cat = $cat[0];
          echo '<li>' .get_category_parents($cat, FALSE, '<li>'); //fails here this is line 835
          the_title(); echo' <strong><span style="font-size : 10px;">* YOU ARE CURRENTLY HERE!</span></strong></li>';
          echo $currentAfter;

    Please someone help!! I am going insane here…

    [No bumping. If it’s that urgent, consider hiring someone.]

  • The topic ‘first wordpress custom post type’ is closed to new replies.