[英]Add ACF Post Object field to Wordpress admin column
I have a custom post type for books that has two ACF fields, book_title
and book_author
.我有一个自定义帖子类型,用于具有两个 ACF 字段book_title
和book_author
。 I have a separate custom post type for passages from the book, which pulls in a book, as an ACF post object, into two fields in the passages custom post type with the same field names.我有一个单独的自定义帖子类型,用于书中的段落,它将一本书作为 ACF 帖子对象拉入段落自定义帖子类型中具有相同字段名称的两个字段中。
I would like to be able to display the book_title
and book_author
fields as columns in the passages custom post type list.我希望能够将book_title
和book_author
字段显示为段落自定义帖子类型列表中的列。 I am currently able to pull in the title of the book, but thats only because I am grabbing the title of the post, not the actual book_title
field from the post.我目前能够提取书名,但这只是因为我正在获取帖子的标题,而不是帖子中的实际book_title
字段。 Is there a way to grab fields from a post object like this and set them as columns?有没有办法从这样的帖子对象中获取字段并将它们设置为列?
Here is my current code from my passages custom post type file:这是我的段落自定义帖子类型文件中的当前代码:
function add_acf_columns($columns)
{
return array_merge($columns, array(
'book_title' => __('Book Title') ,
'book_author' => __('Book Author')
));
}
add_filter('manage_passages_posts_columns', 'add_acf_columns');
function passages_custom_column($column, $post_id)
{
switch ($column)
{
case 'book_title':
echo get_the_title(get_field('book_title', $post_id));
break;
case 'book_author':
echo get_post_meta($post_id, 'book_author', true);
break;
}
}
add_action('manage_passages_posts_custom_column', 'passages_custom_column', 10, 2);
I'm a tad confused on your actual set up, but the general idea should apply here.我对您的实际设置有点困惑,但总体思路应该适用于此。 When using the ACF "Relational > Post Object" field, it stores the actual relative WP_Post
object.当使用ACF“Relational > Post Object”字段时,它存储实际的相关WP_Post
对象。 This allows you to reference anything you want from it without having to invoke anything special.这允许您从它引用您想要的任何内容,而无需调用任何特殊内容。
Just doing the following:只需执行以下操作:
$object = get_field( 'name_of_field', $post_id );
Will give you access to whatever data you want from that post/custom post type:将使您可以从该帖子/自定义帖子类型访问您想要的任何数据:
$title = $object->post_title;
$author_id = $object->post_author;
So, like I mentioned, I'm a bit confused on your actual field names and where the actual object is stored, but I think the same post object is stored in both the book_title
and book_author
fields?所以,就像我提到的,我对你的实际字段名称和实际对象的存储位置有点困惑,但我认为相同的 post 对象存储在book_title
和book_author
字段中? Either way, you just need to get the WP_Post
object with get_field
, and then you'll have access to everything related to it.无论哪种方式,您只需要使用get_field
获取WP_Post
对象,然后您就可以访问与其相关的所有内容。 Then you can ouput the title, and use something like get_the_author_meta()
to get the author's display/first/last name, etc.然后你可以输出标题,并使用诸如get_the_author_meta()
类的东西来获取作者的显示/名字/姓氏等。
add_action('manage_passages_posts_custom_column', 'passages_custom_column', 10, 2);
function passages_custom_column($column, $post_id) {
switch( $column ){
case 'book_title':
$book_object = get_field( 'book_title', $post_id );
echo ($book_object) ? $book_object->post_title : '';
break;
case 'book_author':
$book_object = get_field( 'book_author', $post_id );
if( $book_object ){
echo get_the_author_meta( 'display_name', $book_object->post_author );
}
break;
}
}
I solved this by modifying Xhynk's answer.我通过修改 Xhynk 的答案解决了这个问题。
After you grab the ID of the Book post object in the Passages post book
field, you can use ACF's get_field() to grab the title
and author
values from the Book post itself, and then pass those values into the columns.在 Passages post book
字段中获取 Book post 对象的 ID 后,您可以使用ACF 的 get_field()从 Book post 本身获取title
和author
值,然后将这些值传递到列中。
// add columns to passages list
add_filter('manage_passages_posts_columns', 'add_acf_columns');
function add_acf_columns($columns)
{
return array_merge($columns, array(
'book_title' => __('Book Title') ,
'book_author' => __('Book Author')
));
}
// add title and author to passages list columns
add_action('manage_passages_posts_custom_column', 'passages_custom_column', 10, 2);
function passages_custom_column($column, $post_id) {
switch( $column ){
case 'book_title':
$book_object = get_field( 'book', $post_id );
$book_title = get_field('title', $book_object->ID);
echo ($book_title) ? $book_title : '';
break;
case 'book_author':
$book_object = get_field( 'book', $post_id );
$book_author = get_field('author', $book_object->ID);
echo ($book_author) ? $book_author : '';
break;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.