How to Display Custom meta field value insted of t

2019-07-21 09:49发布

I have created a custom post type "cinfo" and removed title and editor form the edit page. With the help of this code. Also displayed some custom meta fields which are relevant to my plugin.

function remove_box(){
    remove_post_type_support('cinfo', 'title');
    remove_post_type_support('cinfo', 'editor');
}
add_action("admin_init", "remove_box");

It looks something like this.

Custom post type edit page

Now when i see the list page I still see the title with edit, view and delete button beneath it. which I don't want because the title field doesn't exist in the edit page So it looks a bit irrelevant in the listing page. Instead of that I tried to display the custom meta field "email" but I was only able to change the heading of the column. which looks something like this.

Cards info listing page

I just did some research and found one action and filter but they still didn't seems to be much of a help to me. Still for the better view of the problem. Also I tried to use a plugin Post List View Count but it also didn't accomplish my purpose. I hope You understand what I basically want to do. Thanks for your time to read my question.

add_filter('manage_cinfo_posts_columns', 'bs_cinfo_table_head');
function bs_cinfo_table_head( $defaults ) {
    $defaults['title']  = 'Email';
    return $defaults;
}


add_action( 'manage_cinfo_posts_custom_column', 'card_cinfo_table_content', 10, 2 );
function card_cinfo_table_content( $column_name, $post_id ) {
    if ($column_name == 'title') {
        echo "its working";
    }
}

1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-07-21 10:38

The action manage_cinfo_posts_custom_column will never be true. It's better to remove the defaults on manage_cinfo_posts_columns and do the regular custom stuff in the other filter.

I tried this with a fast class to test all together inside my setup:

class SO23467344
{
    private $cpt = 'portfolio';
    private $custom_field = 'video';

    public function __construct()
    {
        add_filter( "manage_edit-{$this->cpt}_columns", array( $this, 'column_register' ), 20, 1 );
        add_action( "manage_{$this->cpt}_posts_custom_column", array( $this, 'column_display' ), 20, 2 );
        add_action( "admin_init", array( $this, "remove_box" ) );
    }
    function column_register( $columns ) 
    {
        $columns['my-col'] = 'My column';
        # Remove default columns
        unset( $columns['title'], $columns['categories'], $columns['comments'], $columns['date'] );         
        return $columns;
    }

    function column_display( $column_name, $post_id ) 
    {
        if ( 'my-col' != $column_name )
            return;

        if ( $field = get_post_meta( $post_id, $this->custom_field, true ) )
            echo '<br/><strong style="color:#0f0;font-size:4em"> • </strong>';
    }

    function remove_box(){
        remove_post_type_support( $this->cpt, 'title' );
        remove_post_type_support( $this->cpt, 'editor' );
    }
}
new SO23467344;
查看更多
登录 后发表回答