我目前正在遵循指南,并设法创建了自定义帖子类型和元框。然而,让元数据显示在帖子中对我来说目前还没有发生。
我想要的只是元框有 3 个自定义文本字段,然后我可以在帖子中输出。
这是我的函数文件(定义帖子类型和元框的部分):
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"]);
}
}
?>
我在单个产品中显示信息,如下所示:
<?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; ?>
我知道我可能在做一些愚蠢的事情,但任何帮助都会非常感激。我知道我可以使用插件来做到这一点,但我宁愿学习以正确的方式做到这一点。