简体   繁体   English

将 ACF 管理列添加到自定义帖子类型

[英]Add ACF admin column to custom post type

I'm trying to add a new admin column to my custom post type using the content of a custom field (ACF).我正在尝试使用自定义字段 (ACF) 的内容向我的自定义帖子类型添加一个新的管理列。 The field I want to add is a 'post object' field, but it just displays the post title instead of the linked post from the ACF.我要添加的字段是“帖子对象”字段,但它只显示帖子标题而不是来自 ACF 的链接帖子。 I added a screenshot.我添加了一个屏幕截图。

我现在拥有的

This is what I have so far:这是我到目前为止:

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' );

Try changing your add_new_realisaties_admin_column_show_value function to the code below.尝试将您的add_new_realisaties_admin_column_show_value函数更改为以下代码。 If the ACF field name is realisatie_line you are going to need to also pass a $post_id to get the specific meta data back for each particular post.如果 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;

    }
}

There's a couple things I notice.我注意到几件事。 One is that you shouldn't actually need to use the setup_postdata() function, because the ACF Post Object field uses get_post() which returns a full object already.一是您实际上不需要使用setup_postdata()函数,因为ACF Post Object 字段使用get_post()已经返回完整对象。 You'd only do this if you're intended to override the global $post object, say on a single-{post_type}.php template, for instance.仅当您打算覆盖全局$post对象时才这样做,例如在single-{post_type}.php模板上。

Another thing is that it's generally more common to use a switch statement instead of an if/else for post columns.另一件事是,对于 post 列,通常更常见的是使用switch语句而不是if/else A bit pedantic, but something to note.有点迂腐,但有一点需要注意。

Lastly, the_title() will echo the title by default, so assigning it, and then echoing it later, can cause issues (namely leaving variables littered around the document).最后,默认情况下the_title()将回显标题,因此分配它,然后在以后回显它可能会导致问题(即在文档周围留下乱七八糟的变量)。 Consider using get_the_title() if you plan on assigning it to a variable.如果您计划将其分配给变量,请考虑使用get_the_title() Also, I won't go into excruciating detail, but just using setup_postdata may not be enough to get the post helper functions to pull the data from where you want.此外,我不会详细介绍令人痛苦的细节,但仅使用setup_postdata可能不足以让 post helper 函数从您想要的位置提取数据。

Now, with all that said, you should be able to just echo the post_title field of the $post_object fromget_field() , since it returns a full formed WP_Post object .现在, $post_object ,您应该能够从get_field()回显$post_objectpost_title字段,因为它返回一个完整的 WP_Post object I put this on a test site of mine and it worked just as intended:我把它放在我的一个测试站点上,它按预期工作:

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;
    }
}

And here's what it looks like in the admin, note I just grabbed a random post for the Post Object relational field:这是管理员中的样子,请注意,我刚刚为 Post Object 关系字段抓取了一个随机帖子:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WordPress ACF:在管理部分中为自定义帖子类型添加默认行到Repeater字段类型 - WordPress ACF: add default rows to Repeater field type in admin section for a custom post type 将 ACF Post Object 字段添加到 Wordpress 管理列 - Add ACF Post Object field to Wordpress admin column 标题自定义帖子类型 ACF - Title custom post type ACF 自定义帖子类型中的WordPress添加下拉列表在admin中添加/编辑帖子 - Wordpress Add dropdown in custom post type add/edit post in admin 在 Wordpress 管理员中使用 ACF Pro 选择字段的可排序自定义列用于帖子列表 - Sortable custom column using ACF Pro select field in Wordpress admin for post list 自定义帖子类型的自定义分类未显示在 Wordpress 管理列中 - Custom Taxonomies for custom post type not showing up in Wordpress Admin column 使用 ACF 将内容添加到帖子类型存档 - Use ACF to add content to post type archive 如何在 Wordpress 中添加短代码以使用 ACF 查询自定义帖子类型? - How can I add a shortcode to query Custom Post Type with ACF in Wordpress? 将ACF字段的值添加到当前“自定义帖子”类型页面中的“重力表单”中 - Add value of ACF field to Gravity Form in current Custom Post type page 如何在管理菜单中添加自定义帖子类型? - How do I add a custom post type to the admin menu?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM