2013-07-25 39 views
2

我正在跟随一个指南,并设法创建一个自定义帖子类型和元框。但是,获取元数据显示在帖子中,目前并没有发生。努力获得自定义元数据显示在wordpress后

我想要的是元框中有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; ?> 

我知道我可能在做一些愚蠢的事情,但任何帮助将非常感激。我知道我可以用插件做到这一点,但我宁愿学会按照正确的方式做到这一点。

回答

1

你只是有错误的ID在地方

所以输出正确的应该是:

<?php 
$custom = get_post_custom($post->ID); 
$buynowlink = $custom ["buylink"][0]; 
$price = $custom ["price"][0]; 
$previewlink = $custom ["previewlink"][0]; 
?> 

,并打印出来 - 你有购买链接的预览规则以及作为一个开放的p标签,所以应该是:

<?php print "<p>$buynowlink</p>"; ?> 
    <?php print "<p>$price</p>"; ?> 
    <?php print "<p>$previewlink</p>"; ?> 

希望这有助于

1

我想我会在代码中看到一些语法错误,例如您希望打印自定义字段值的方式等。

我会推荐这种方法,而不是你正在使用的方法,我会在代码后解释为什么。

PHP WordPress的循环与get_post_meta();

<?php if (have_posts()) : while (have_posts()) : the_post(); ?> 

    <!--This is a conditional statement to see if the value of the custom field has something, and if it does then it shows it, otherwise it doesn't render anything--> 

    <?php if (get_post_meta($post->ID, 'cf-size', true)):?> 
    <h1>Custom Field Value: <?php echo get_post_meta($post->ID, 'cf-size', true);?></h1> 
    <?php endif;?> 

<?php endwhile; endif; ?> 

你可以看到,cf-size是自定义字段的名称和我检查它在当前岗位价值。上面的代码肯定会起作用,因为我在自己的作品中多次使用过它。

下面是如何使用相同的if语句来拉动3个字段的示例......请记住,如果条件未验证为“true”(意味着所有3个字段都有值),则不会显示即使2或1确实有价值。

<?php if (have_posts()) : while (have_posts()) : the_post(); ?> 

    <!--This is a conditional statement to see if the value of the custom field has something, and if it does then it shows it, otherwise it doesn't render anything--> 

    <?php if (get_post_meta($post->ID, 'cf-size', 'cf-color', 'cf-brand', true)):?> 
    <h1>Custom Field Value for Size: <?php echo get_post_meta($post->ID, 'cf-size', true);?></h1> 
    <h1>Custom Field Value for Color: <?php echo get_post_meta($post->ID, 'cf-color', true);?></h1> 
    <h1>Custom Field Value for Brand: <?php echo get_post_meta($post->ID, 'cf-brand', true);?></h1> 
    <?php endif;?> 

<?php endwhile; endif; ?> 
相关问题