2013-10-09 48 views
-1

請幫我一把,我三天以來一直在作戰。在WordPress的frontent獲取圖像自定義字段的值?

如何獲取自定義字段的值以將其顯示在帖子內容區域中?

+0

圖片自定義字段?你在哪裏存儲文件信息和圖像。 get_post_meta。可以幫助你帶來定製後的價值meta – Gowri

+0

感謝您的答覆。我在function.php中添加了一些代碼,通過它我可以看到我的自定義字段,同時上傳任何圖像,它保留了我給它的那些值,但是我想要的是將自定義字段值提取到我的帖子中。嘗試了很多東西但沒有結果。 – shweta

+0

首先你沒有回答我的問題。你嘗試過'get_post_meta'嗎?你能解釋一下你嘗試過很多東西嗎 – Gowri

回答

0

你可以自定義字段使用下面的代碼顯示:

get_post_meta($post_id, $key, $single); 
+0

add_filter('attachment_fields_to_edit','edit_media_custom_field',11,2); add_filter('attachment_fields_to_save','save_media_custom_field',11,2); function edit_media_custom_field($ form_fields,$ post){ $ form_fields ['custom_field'] = array('label'=>'Product Code','input'=>'text','value'=> get_post_meta($後 - > ID,'_custom_field',true)); return $ form_fields; } function save_media_custom_field($ post,$ attachment){ update_post_meta($ post ['ID'],'_custom_field',$ attachment ['custom_field']); return $ post; – shweta

+0

,我想在category.php中獲取它。我正在使用 echo get_post_meta($ post-> ID,'_custom_field',true); – shweta

+0

$ key:它將是您的自定義字段的名稱 –

0

自定義字段中的數據可以在裏面模板中獲取(內循環),使用此功能:

獲取所有領域

<?php the_meta(); ?> 

或對於特定值

get_post_meta($post_id, $key, $single); 

您可以在官方wordpress文檔中瞭解更多有關custom fields的示例。這裏

<?php echo get_post_meta($post_id, $key, true); ?> 

您的參數必須同你提到的function.php,同時創造等作爲您的自定義字段:

+0

好的非常感謝:) – shweta

0

您可以通過下面的代碼中使用此

<?php add_post_meta($post_id, $key, $single,true); ?> 

謝謝。

+0

但這顯示自定義字段w.r.t發佈。我想要w.r.t image.Can我? – shweta

+0

可以請你分享你的自定義字段的代碼..? –

相關問題