• Hi,

    I am trying to use the custom post types to make a portfolio for my former works.

    What I have done so far is adding a custom post type where I will store all my data about diffrent projects I have been working with.

    So far the code for the custom post type in the functions.php file looks like this: `add_action( ‘init’, ‘create_post_types’ );

    function create_post_types() {
    register_post_type( ‘project’,
    array(
    ‘labels’ => array(
    ‘name’ => __( ‘Projects’ ),
    ‘singular_name’ => __( ‘Project’ ),
    ‘add_new’ => __( ‘Add New Project’ ),
    ‘add_new_item’ => __( ‘Add New Project’ ),
    ‘edit_item’ => __( ‘Edit Project’ ),
    ‘new_item’ => __( ‘Add New Project’ ),
    ‘view_item’ => __( ‘View Project’ )
    ),
    ‘public’ => true,
    ‘menu_position’ => 5,
    ‘supports’ => array( ‘title’, ‘editor’, ‘thumbnail’, ‘page-attributes’ ),
    ‘rewrite’ => array( ‘slug’ => ‘project’, ‘with_front’ => false ),
    ‘register_meta_box_cb’ => ‘add_projects_metaboxes’
    )
    );
    }

    function add_projects_metaboxes() {
    add_meta_box(‘project_description’, ‘Description’, ‘project_description’, ‘project’, ‘normal’, ‘high’);
    }

    function project_description() {
    global $post;

    // Noncename needed to verify where the data originated
    echo ‘<input type=”hidden” name=”projectmeta_noncename” id=”projectmeta_noncename” value=”‘ .
    wp_create_nonce( plugin_basename(__FILE__) ) . ‘” />’;

    // Get the description data if its already been entered
    $description = get_post_meta($post->ID, ‘_description’, true);
    $details = get_post_meta($post->ID, ‘_customer’, true);
    $principal = get_post_meta($post->ID, ‘_principal’, true);
    $link = get_post_meta($post->ID, ‘_link’, true);

    // Echo out the field
    echo ‘<p>Description</p>’;
    echo ‘<textarea name=”_description” cols=”40″ rows=”5″ style=”width: 99%;”>’ . wpautop($description) . ‘</textarea>’;
    echo ‘<p>Customer <i>As in the customer that will use the project</i></p>’;
    echo ‘<input type=”text” name=”_customer” value=”‘ . $customer . ‘” style=”width: 99%;” />’;
    echo ‘<p>Principal</p>’;
    echo ‘<input type=”text” name=”_details” value=”‘ . $principal . ‘” style=”width: 99%;” />’;
    echo ‘<p>Link: <i>http://www.link.com</i></p>&#8217;;
    echo ‘<input type=”text” name=”_details” value=”‘ . $link . ‘” style=”width: 99%;” />’;
    }

    function save_project_meta($post_id, $post) {

    // verify this came from the our screen and with proper authorization,
    // because save_post can be triggered at other times
    if ( !wp_verify_nonce( $_POST[‘projectmeta_noncename’], plugin_basename(__FILE__) )) {
    return $post->ID;
    }

    // Is the user allowed to edit the post or page?
    if ( !current_user_can( ‘edit_post’, $post->ID ))
    return $post->ID;

    // OK, we’re authenticated: we need to find and save the data
    // We’ll put it into an array to make it easier to loop though.

    $project_description[‘_description’] = $_POST[‘_description’];
    $project_description[‘_customer’] = $_POST[‘_customer’];
    $project_description[‘_principal’] = $_POST[‘_principal’];
    $project_description[‘_link’] = $_POST[‘_link’];

    // Add values of $project_description as custom fields

    foreach ($project_description as $key => $value) { // Cycle through the $project_description array!
    if( $post->post_type == ‘revision’ ) return; // Don’t store custom data twice
    $value = implode(‘,’, (array)$value); // If $value is an array, make it a CSV (unlikely)
    if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
    update_post_meta($post->ID, $key, $value);
    } else { // If the custom field doesn’t have a value
    add_post_meta($post->ID, $key, $value);
    }
    if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
    }

    }

    add_action(‘save_post’, ‘save_project_meta’, 1, 2); // save the custom fields

    if ( function_exists( ‘add_theme_support’ ) ) {
    add_theme_support( ‘post-thumbnails’ );
    add_image_size(‘Home Thumbnail’, 300, 300, true);
    }`

    This make my Admin-panel have some extra fields where I can put in eg. The projects name, the principal of the project, a link to the project and so on.

    My question is now, How do I echo this information out in my template files?

    For example, what do I write to echo the projects name out where I want it.

    To echo the normal information out I just write:
    <?php the_content(); ?>

    But what about the other information that I have saved in my project?

    Best regards
    André

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Custom post types: Show data from custom fields’ is closed to new replies.