2013-07-25 36 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; ?> 
相關問題