TransWikia.com

How to add 'total' value to custom column title on the posts list page

WordPress Development Asked on October 30, 2021

I have added custom column to all posts page by this code:

// ADD NEW COLUMN
function len_columns_head($defaults) {
    $defaults['post_len'] = 'Symbols';
    return $defaults;
}
 
// SHOW POST LENGTH
function len_columns_content($column_name, $post_ID) {
    if ($column_name == 'post_len') {
        $post    = get_post($post_ID); 
        $content = $post->post_content;
        $length  = strlen($content);
        if ($length) {
            echo $length.' symbols';
        }
    }
}

add_filter('manage_posts_columns', 'len_columns_head');
add_action('manage_posts_custom_column', 'len_columns_content', 10, 2);

but now i need to add total count of symbols to column title like this: "Symbols, 123456 total". How can i achieve this? I have tried to add global variable to functions.php and add $length to it on every len_columns_content call, but that didn’t work

One Answer

WebElaine, if you are asking about global variable, i am doing something like this:

global $total_len;

...
function len_columns_content($column_name, $post_ID) {
...
if ($length) {
    $total_len += $length;
    echo $length.' symbols'.'total: '.$total_len;
}
...

and in every string i have get $total_len equal $length, as if $total_len is not global

Answered by sock4proxy on October 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP