I’m following a guide at the moment and have managed to create a custom post type and meta boxes. However, getting the meta data to display in a post just isn’t happening for me at the moment.
All i want is the meta box to have 3 custom text fields that I can then output in a post.
This is my functions file (the part that defines the post type and meta boxes):
add_action('init', 'product_manager_register'); // Calls function to set up post-type
function product_manager_register() {
$args = array(
'label' => __('Product Manager'),
'singular_label' => __('Skin'),
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => true,
'has_archive' => true,
'supports' => array('title', 'editor', 'thumbnail'),
'rewrite' => array('slug' => 'skins', 'with_front' => false),
);
register_post_type('products' , $args ); // runs the function
}
//Add featured image support to the theme
if (function_exists('add_theme_support')) {
add_theme_support('post-thumbnails');
}
add_action("admin_init", "product_manager_add_meta");
function product_manager_add_meta(){
add_meta_box("product-meta", "Product Options", "product_manager_meta_options", "products", "normal", "high");
}
function product_manager_meta_options(){
global $post;
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;
$custom = get_post_custom($post->ID);
$buylink = $custom['buylink'][0];
$price = $custom['price'][0];
$previewlink = $custom['previewlink'][0];
?>
<style type="text/css">
<?php include('productmeta.css'); ?>
</style>
<div class="product_extras">
<div> <label> Buy Link: </label> <input name="buylink" value="<?php echo $buylink; ?>" /></div>
<div> <label> Price: </label> <input name="price" value="<?php echo $price; ?>" /></div>
<div> <label> Preview Link: <input name="previewlink" value="<?php echo $previewlink; ?>" /></div>
</div>
<?php
}
add_action('save_post', 'save_product_meta');
function save_product_meta(){
global $post;
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ){
return $post_id;
}else{
update_post_meta($post->ID, "buylink", $_POST["buylink"]);
update_post_meta($post->ID, "price", $_POST["price"]);
update_post_meta($post->ID, "previewlink", $_POST["previewlink"]);
}
}
?>
And I display the information in single-products like this:
<?php get_header() ?>
<div class="container content"><!-- Begin maincontent Container -->
this is the page i'm editing
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php
$custom = get_post_custom($post->ID);
$buynowlink = $custom ["buynowlink"][0];
$price = $custom ["price"][0];
$previewlink = $custom ["previewlink"][0];
?>
<div class="post group">
<h2><?php the_title(); ?> </h2>
<?php the_content(); ?>
<?php print "<p>$buynowlink</p>"; ?>
<?php print "<p>$price/p>"; ?>
<?php print "<p>$buynowlink</p>"; ?>
</div>
<?php endwhile; else: ?>
<?php endif; ?>
I know I’m probably doing something stupid but any help would be really appreciated. I know I could do this with a plugin but I’d rather learn to do it the proper way.
You just have the wrong ID’s in place
so to output correctly its should be:
and to print it – you have the buy link in the previews rule as well as an open p tag so it should be:
Hope this helps
I think I see a couple of syntax errors in your code, such as the way you want to print the custom field’s values and so on.
I would recommend this approach as opposed to the approach you are using, and I’ll explain why in a bit, after the code.
PHP WordPress Loop with
get_post_meta();
You can notice that
cf-size
is the name of the custom field and that I’m checking for it’s value on the current post. The code above will surely work as I’ve used it many times in my own creations.Here is an example of how to pull 3 fields with the same if statement… just remember that if the condition doesn’t validate as “true” (meaning that all 3 fields have a value) then none will show even if 2 or 1 does have a value.