![](/img/trans.png)
[英]WordPress ACF: add default rows to Repeater field type in admin section for a custom post type
[英]Add ACF admin column to custom post type
我正在嘗試使用自定義字段 (ACF) 的內容向我的自定義帖子類型添加一個新的管理列。 我要添加的字段是“帖子對象”字段,但它只顯示帖子標題而不是來自 ACF 的鏈接帖子。 我添加了一個屏幕截圖。
這是我到目前為止:
function add_new_realisaties_column($columns) {
$columns['realisatie_line'] = 'Line';
return $columns;
}
add_filter('manage_realisaties_posts_columns', 'add_new_realisaties_column');
function add_new_realisaties_admin_column_show_value( $column, $post_id ) {
if ($column == 'realisatie_line') {
$post_object = get_field('realisatie_line');
if( $post_object ):
// override $post
$post = $post_object;
setup_postdata( $post );
$evdate = the_title();
wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif;
echo $evdate;
}
}
add_filter('manage_realisaties_posts_custom_column', 'add_new_realisaties_admin_column_show_value', 10, 2);
/* Make the column sortable */
function set_custom_realisaties_sortable_columns( $columns ) {
$columns['realisatie_line'] = 'realisatie_line';
return $columns;
}
add_filter( 'manage_edit-realisaties_sortable_columns', 'set_custom_realisaties_sortable_columns' );
function realisaties_custom_orderby( $query ) {
if ( ! is_admin() )
return;
$orderby = $query->get('orderby');
if ( 'realisatie_line' == $orderby ) {
$query->set( 'meta_key', 'realisatie_line' );
$query->set( 'orderby', 'meta_value' );
}
}
add_action( 'pre_get_posts', 'realisaties_custom_orderby' );
嘗試將您的add_new_realisaties_admin_column_show_value
函數更改為以下代碼。 如果 ACF 字段名稱是realisatie_line
您還需要傳遞$post_id
以獲取每個特定帖子的特定元數據。
function add_new_realisaties_admin_column_show_value( $column, $post_id ) {
//Try using a switch/case for the column name
switch ( $column ) {
case 'realisatie_line': //Name of new column from add_new_realisaties_column function
echo get_the_title( get_post_meta( $post_id, 'realisatie_line', true ) ); //Getting the ACF post meta using the $post_id, passing it through the get_the_title function to get title of attached post
break;
default:
break;
}
}
我注意到幾件事。 一是您實際上不需要使用setup_postdata()
函數,因為ACF Post Object 字段使用get_post()
已經返回完整對象。 僅當您打算覆蓋全局$post
對象時才這樣做,例如在single-{post_type}.php
模板上。
另一件事是,對於 post 列,通常更常見的是使用switch
語句而不是if/else
。 有點迂腐,但有一點需要注意。
最后,默認情況下the_title()
將回顯標題,因此分配它,然后在以后回顯它可能會導致問題(即在文檔周圍留下亂七八糟的變量)。 如果您計划將其分配給變量,請考慮使用get_the_title()
。 此外,我不會詳細介紹令人痛苦的細節,但僅使用setup_postdata
可能不足以讓 post helper 函數從您想要的位置提取數據。
現在, $post_object
,您應該能夠從get_field()
回顯$post_object
的post_title
字段,因為它返回一個完整的 WP_Post object 。 我把它放在我的一個測試站點上,它按預期工作:
add_filter('manage_realisaties_posts_custom_column', 'add_new_realisaties_admin_column_show_value', 10, 2);
function add_new_realisaties_admin_column_show_value( $column, $post_id ){
switch( $column ){
case 'realisatie_line':
if( $post_object = get_field('realisatie_line') ){
echo $post_object->post_title;
}
break;
}
}
這是管理員中的樣子,請注意,我剛剛為 Post Object 關系字段抓取了一個隨機帖子:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.