我有一個 [基本網格] 網格,它通過一些 [事件日歷] 事件。現在在每個專案中,我想顯示事件開始日期,但是當我使用相關元鍵作為網格專案中的引數源時,我會回傳默認日期格式。事情是我需要以特定方式格式化該日期,而我偶然發現的是這個解決方案:
function tribe_start_date_shortcode() {
global $post;
return tribe_get_start_date( $post->ID, false, 'F j, Y' );
}
add_shortcode('tribe-start-date', 'tribe_start_date_shortcode');
現在,當我在需要它的地方使用短代碼時,日期確實得到了很好的格式化,但問題是我得到的不是事件開始日期,而是當前的 WordPress 發布日期(即網格運行的位置)。
我怎樣才能讓它顯示事件專案的日期?
任何幫助將不勝感激,謝謝!
---------------------------//---------- ------
作業代碼(感謝@Howard E):
// Get post id through the shortcode and return formatted date
function tribe_start_date_shortcode( $atts ) {
$atts = shortcode_atts(array('event' => ''), $atts, 'tribe-start-date');
return tribe_get_start_date( absint( $atts['event'] ), false, 'F j, Y' );
}
// Wordpress shortcode register
add_shortcode('tribe-start-date', 'tribe_start_date_shortcode');
// Place the above in your theme's functions.php
// Actual shortcode to be placed within the Essential Grid Item Skin relevant layer
[tribe-start-date event=%post_id%]
uj5u.com熱心網友回復:
您可以將事件 ID 傳遞給簡碼。全域$post將不會在短代碼函式中檢索 post 物件。
像這樣使用簡碼
[tribe-start-date event=123]
function tribe_start_date_shortcode( $atts ) {
$atts = shortcode_atts(
array(
'event' => '',
),
$atts,
'tribe-start-date',
);
return tribe_get_start_date( absint( $atts['event'] ), false, 'F j, Y' );
}
add_shortcode( 'tribe-start-date', 'tribe_start_date_shortcode' );
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/475535.html
