2013-06-26 36 views
0

在我的WordPress網站上,我有一個自定義字段,我想將我的帖子的摘錄添加到自定義字段值中。如何獲取自定義字段值的摘錄?

我的代碼:

function mk_set_default_custom_fields($post_id) 
{ 
if ($_GET['post_type'] != 'page') { 
    add_post_meta($post_id, 'key', 'value'); 

} 

return true; 
} 

我怎樣才能把我的文章的摘錄到​​3210值一次我打的發佈按鈕?

回答

0

您可以使用$post對象,並用它的幫助,您可以設置value

$post->post_excerpt 

只是爲其他可用選項信息

$post->post_author 
$post->post_date 
$post->post_date_gmt 
$post->post_content 
$post->post_content_filtered 
$post->post_title 
$post->post_excerpt 
$post->post_status 
$post->post_type 
$post->comment_status 
$post->ping_status 
$post->post_password 
$post->post_name 
$post->to_ping 
$post->pinged 
$post->post_modified 
$post->post_modified_gmt 
$post->post_parent 
$post->menu_order 
$post->guid 
+0

感謝您的快速回復,但它不是加工。我需要將arguement $ post添加到函數中嗎?我做到了,並添加了$ post-> post_excerpt作爲值,但它沒有工作,並且我也收到了一條錯誤消息警告:缺少mk_set_default_custom_fields()的參數2並且警告:無法修改頭信息 - 頭文件已經由pluggable.php發送 –

+0

@Denish:你能提一下它的來源嗎? –

0

把數組中所需自定義後這樣in your functions.php

$args = array(
     'supports' => array('title','editor','author','excerpt') // by writing these lines an custom field has been added to CMS 
); 

F或在前端檢索

echo $post->post_excerpt; // this will return you the excerpt of the current post 
0

我不知道爲什麼你會這樣做,因爲這會導致您的內容重複。但是,它看起來像你的功能掛鉤到save_post。如果是這種情況,您可以從$_POST['post_except']變量中獲得除外。只是不要認爲總是會設置該變量,因爲save_post在幾種不同的情況下被調用。

0

functions.php

add_action('save_post', 'my_custom_field_save'); 
function my_custom_field_save($post_id) 
{ 
    if ($_POST['post_type'] == 'post') { 
     add_post_meta($post_id, 'custom_excerpt_field', get_the_excerpt($post_id), true); 
    } 
} 

這將save/update自定義字段(custom_excerpt_field),每次添加/更新發布時間加入此。 在前端,以獲得自定義字段,使用(內環路時)

$custom_excerpt_field_data = get_post_meta(get_the_ID(), 'custom_excerpt_field', true); 

使用這個(當環路外)

global $post; 
$custom_excerpt_field_data = get_post_meta($post->ID, 'custom_excerpt_field', true); 
相關問題