2016-12-07 44 views
1

我在WP中很新,我想更改門戶上的標題顯示以使用過濾器顯示括號內的發佈日期?我該怎麼做 ? 當我嘗試這個(@Dre解決方案);我得到的也最新與頂部的菜單:在Wordpress中顯示標題中的發佈日期

function my_add_date_to_title($title, $id) { 
    $date_format = get_option('date_format'); 
    $date = get_the_date($date_format, $id); // Should return a string 
    return $title . ' (' . $date . ')'; 
} 
add_filter('the_title','my_add_date_to_title',10,2); 

enter image description here

回答

1

你可能會更好編輯頁面模板簡單地輸出的日期;它速度更快,並且使得以後更容易找到。通過過濾器應用內容可能會使跟蹤器變得更加困難其中內容來自。

說了這麼多,如果你決定通過過濾器來做到這一點,下面是你需要添加到您的functions.php文件的內容:

/* Adds date to end of title 
* @uses Hooked to 'the_title' filter 
* @args $title(string) - Incoming title 
* @args $id(int) - The post ID 
*/ 
function my_add_date_to_title($title, $id) { 

    // Check if we're in the loop or not 
    // This should exclude menu items 
    if (!is_admin() && in_the_loop()) { 

     // First get the default date format 
     // Alternatively, you can specify your 
     // own date format instead 
     $date_format = get_option('date_format'); 

     // Now get the date 
     $date = get_the_date($date_format, $id); // Should return a string 

     // Now put our string together and return it 
     // You can of course tweak the markup here if you want 
     $title .= ' (' . $date . ')'; 
    } 

    // Now return the string 
    return $title; 
} 

// Hook our function to the 'the_title' filter 
// Note the last arg: we specify '2' because we want the filter 
// to pass us both the title AND the ID to our function 
add_filter('the_title','my_add_date_to_title',10,2); 

沒有測試,但應該工作。

+0

您的解決方案的作品,但我也看到與頂級菜單元素的日期! – user2997418

+0

@ user2997418啊,是的,我忘了那些。我已經更新它來解釋它。 – Dre

+0

謝謝,現在作品完美 – user2997418