![](/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.