• Resolved ddesmond

    (@ddesmond)


    Hi,

    I am attempting to add a new custom column to the ‘Display fields’. I found a page in your documentation with a snippet for adding the “User name” column to catfolders. However, the page only contains a screenshot of the snippet and not the actual code.

    I copied the code from your screenshot into my functions.php, but I am encountering the following error: “DataTables warning: table id=catf-dg-table-0 – Incorrect column count. For more information about this error, please see https://datatables.net/tn/18.”

    Can you please help me identify what might be wrong with the code snippet? Also, where can I find the rest of your code snippets?

    Thank you.

    The page I need help with: [log in to see the link]

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Support mialewp

    (@mialewp)

    Hello @ddesmond,

    Thanks for reaching out.

    Please try using the following code

    // The hook used to add a new column add_filter( 'catf_dg_columns', 'catf_dg_add_column', 10, 1 ); /** * Add a new column * * @param array $columns An array used to define label and key of column * @return array */ function catf_dg_add_column( $columns ) { //Add label and key for new column here $columns[] = array( 'label' => __( 'User name', 'catfolders-document-gallery' ), 'key' => 'user_name', ); return $columns; } // The hook used to add content of column add_filter( 'catf_dg_columns_html', 'catf_dg_add_column_html', 10, 3 ); /** * Add content of columns * * @param array $columns Label and key of column * @param array $file Info of file * @param array $attr Attributes of table * @return array */ function catf_dg_add_column_html( $columns, $file, $attr ) { $id = get_the_ID(); $author_id = get_post_field( 'post_author', $id); $display_name = get_the_author_meta( 'display_name', $author_id ); //Content of column if ( 'user_name' === $columns['key'] ) { ?> <td> <div class=""><?php echo esc_html($display_name); ?></div> </td> <?php } return $columns; }

    Thread Starter ddesmond

    (@ddesmond)

    Thank you this worked perfectly and shows the user name!

    I’m hoping to extend this code to display the value of a custom field I’ve added to my media files using ACF. The field will contain a year value (2020, 2021, 2022, etc).

    For this example I’ve created a custom field named test_field, and modified your code but the column remains empty. Could you please assist me?

    // The hook used to add a new column
    add_filter("catf_dg_columns", "catf_dg_add_column", 10, 1);

    /**
    * Add a new column
    *
    * @param array $columns An array used to define label and key of column
    * @return array
    */
    function catf_dg_add_column($columns)
    {
    // Add label and key for new column here
    $columns[] = [
    "label" => __("Test Field", "catfolders-document-gallery"),
    "key" => "test_field",
    ];
    return $columns;
    }

    // The hook used to add content of column
    add_filter("catf_dg_columns_html", "catf_dg_add_column_html", 10, 3);

    /**
    * Add content of columns
    *
    * @param array $columns Label and key of column
    * @param array $file Info of file
    * @param array $attr Attributes of table
    * @return array
    */
    function catf_dg_add_column_html($columns, $file, $attr)
    {
    $id = get_the_ID();
    $test_field_value = get_field("test_field", $id);

    // Content of column
    if ("test_field" === $columns["key"]) { ?>
    <td>
    <div class=""><?php echo esc_html($test_field_value); ?></div>
    </td>
    <?php }
    return $columns;
    }

    Thank you!!

    Plugin Support alina98

    (@alina98)

    Hi @ddesmond ,

    Please give us your edited code, so we can try to assist in modifying it.

    Thank you

    Thread Starter ddesmond

    (@ddesmond)

    Thanks so much for getting back to me, I appreciate it. Below is my code. It is a modified version of the working code you supplied. However, I may not have updated it incorrectly:

    // The hook used to add a new column
    add_filter("catf_dg_columns", "catf_dg_add_column", 10, 1);

    /**
    * Add a new column
    *
    * @param array $columns An array used to define label and key of column
    * @return array
    */
    function catf_dg_add_column($columns)
    {
    // Add label and key for new column here
    $columns[] = [
    "label" => __("Test Field", "catfolders-document-gallery"),
    "key" => "test_field",
    ];
    return $columns;
    }

    // The hook used to add content of column
    add_filter("catf_dg_columns_html", "catf_dg_add_column_html", 10, 3);

    /**
    * Add content of columns
    *
    * @param array $columns Label and key of column
    * @param array $file Info of file
    * @param array $attr Attributes of table
    * @return array
    */
    function catf_dg_add_column_html($columns, $file, $attr)
    {
    $id = get_the_ID();
    $test_field_value = get_field("test_field", $id);

    // Content of column
    if ("test_field" === $columns["key"]) { ?>
    <td>
    <div class=""><?php echo esc_html($test_field_value); ?></div>
    </td>
    <?php }
    return $columns;
    }

    What I am trying to do is add an extra column that contains the value of an Advanced Custom Field (named test_field in this case).

    Adding custom fields to the document gallery would be an incredibly useful feature! I have a significant number of documents tagged with custom fields. Being able to display and sort by these values would greatly enhance this plugin!

    Thanks again

    Thread Starter ddesmond

    (@ddesmond)

    Hello again,

    I have one more idea. I am hoping you can assist if possible.

    All of my documents are organised into parent child subfolders e.g.

    – Movies
    — 2022
    — 2023
    — 2024

    Could we add a “Subfolder” column to the document gallery? This would solve my problem and also eliminate the need for a custom field and save me from entering the subfolder information for each media file individually.

    Thanks!

    Plugin Support mialewp

    (@mialewp)

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.