2013-03-09 34 views
3

我編碼一個插件,我用簡短的代碼,以呈現頁面內容更改頁面標題。我想更改頁面標題。我用這個:用一小段代碼

// Change the title of the page of the charts to add the current month. 
add_filter('the_title', 'charts_title', 10, 2); 
function charts_title($title, $id) { 
    $title .= ' ' . date('F Y'); 
    return $title; 
} 

但它是這樣做的所有職位和頁面。我可以只爲包含我創建的短代碼的頁面做到這一點嗎?我試圖做到這一點,但它不起作用。

add_shortcode('charts-vote', 'charts_vote'); 
function charts_vote($atts) { 
    // Add the filter only in the short code function callback. 
    add_filter('the_title', 'charts_title', 10, 2); 

    // ... // 
    return $content; 
} 

有人能幫我嗎?

+0

你的簡碼呈現。一般來說,這就是短代碼的工作方式。也就是說,它們在'the_content'運行時渲染。這將是棘手的。 – 2013-03-09 15:59:54

回答

2

據我所知,你設立的細節可能需要檢查的簡碼,但也許可以用於自定義字段:

add_filter('the_title', 'charts_title_so_15312385', 10, 2); 

function charts_title_so_15312385($title, $post_id) 
{ 
    // Admin area, bail out 
    if(is_admin()) 
     return $title; 

    // Custom field not set, bail out 
    $mod_title = get_post_meta($post_id, 'mod_title', true); 
    if(!$mod_title) 
     return $title; 

    // Ok, modify title 
    $title .= ' ' . date('F Y'); 
    return $title; 
} 

短碼甚至可以「對話」與自定義字段爲擴展配置。

爲了方便使用,你可以做一個Custom Meta Box或使用插件像Advanced Custom Fields。頁頭打印完之後,

+1

謝謝你,先生,你是一個生命的救星:) – 2013-03-09 20:16:00

+0

很樂意幫忙,先生巴蒂斯特;) – brasofilo 2013-03-09 20:23:38