最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Showing an ACF field in admin posts dashboard

programmeradmin2浏览0评论

Looking for a way to display a post's ACF data in admin columns using manage_posts_columns hook.

In my functions.php I have this:

function custom_columns( $columns ) {
    $columns = array(
        'cb' => '<input type="checkbox" />',
        'title' => 'Title',
        'featured_image' => 'Image',
        'categories' => 'Categories',
        'amazon_url' => 'Amazon Link',
        'comments' => '<span class="vers"><div title="Comments" class="comment-grey-bubble"></div></span>',
        'date' => 'Date'
     );
    return $columns;
}

add_filter('manage_posts_columns' , 'custom_columns');

function custom_columns_data( $column, $post_id ) {
    switch ( $column ) {
    case 'featured_image':
        the_post_thumbnail( 'thumbnail' );
        break;
    case 'amazon_url' :
         echo get_field( 'product_url', $post_id );
         break;
    }
}

add_action( 'manage_posts_custom_column' , 'custom_columns_data', 10, 2 ); 

I want the ACF field which is product_url for each post appear in the admin dashboard of the post list, the column 'Amazon Link' is there however it seems like the data isn't getting there at all.

Looking for a way to display a post's ACF data in admin columns using manage_posts_columns hook.

In my functions.php I have this:

function custom_columns( $columns ) {
    $columns = array(
        'cb' => '<input type="checkbox" />',
        'title' => 'Title',
        'featured_image' => 'Image',
        'categories' => 'Categories',
        'amazon_url' => 'Amazon Link',
        'comments' => '<span class="vers"><div title="Comments" class="comment-grey-bubble"></div></span>',
        'date' => 'Date'
     );
    return $columns;
}

add_filter('manage_posts_columns' , 'custom_columns');

function custom_columns_data( $column, $post_id ) {
    switch ( $column ) {
    case 'featured_image':
        the_post_thumbnail( 'thumbnail' );
        break;
    case 'amazon_url' :
         echo get_field( 'product_url', $post_id );
         break;
    }
}

add_action( 'manage_posts_custom_column' , 'custom_columns_data', 10, 2 ); 

I want the ACF field which is product_url for each post appear in the admin dashboard of the post list, the column 'Amazon Link' is there however it seems like the data isn't getting there at all.

Share Improve this question asked Sep 4, 2018 at 13:21 Deepak KamatDeepak Kamat 1481 silver badge11 bronze badges 1
  • While posting this I noticed that I had the spelling of amazon incorrect in case 'anazon..' so I thought it was the problem, but I just fixed the mistake and its still the same. – Deepak Kamat Commented Sep 4, 2018 at 13:24
Add a comment  | 

1 Answer 1

Reset to default 1

This code worked perfectly fine:

  function custom_columns( $columns ) {
        $columns = array(
            'cb' => '<input type="checkbox" />',
            'title' => 'Title',
            'featured_image' => 'Image',
            'categories' => 'Categories',
            'amazon_url' => 'Amazon Link',
            'comments' => '<span class="vers"><div title="Comments" class="comment-grey-bubble"></div></span>',
            'date' => 'Date'
         );
        return $columns;
    }

    add_filter('manage_posts_columns' , 'custom_columns');

    function custom_columns_data( $column, $post_id ) {
        switch ( $column ) {
        case 'featured_image':
            the_post_thumbnail( 'thumbnail' );
            break;
        case 'amazon_url' :
             echo get_field( 'product_url', $post_id );
             break;
        }
    }

    add_action( 'manage_posts_custom_column' , 'custom_columns_data', 10, 2 ); 

I realize that there was a spelling error at

case 'anazon_url' : (I updated it in the question though)

It was case 'anazon_url' :

Even after fixing the typo it didn't work but then I cleared caches and tried again and it was working!

So I am leaving the code here so if anyone wants to display ACF data in admin columns here you go!

发布评论

评论列表(0)

  1. 暂无评论