【问题标题】:Adding custom columns with the value of the last user who modified the page to the WordPress admin panel, Pages management将具有最后修改页面的用户的值的自定义列添加到 WordPress 管理面板,页面管理
【发布时间】:2021-05-30 06:10:01
【问题描述】:

我想在 WordPress 的 Pages 表格视图中有两个自定义列。我可以通过内置函数和以下代码为“上次修改”做到这一点:

add_filter('manage_pages_columns','add_custom_page_columns');
function add_custom_page_columns( $columns ) {
    $columns['last_modified'] = 'Last modified';
    $columns['modified_author'] = 'Modified by';
    return $columns;
}

add_action( 'manage_pages_custom_column','custom_columns_content', 10, 2 );
function custom_columns_content ( $column_id, $post_id ) {
    switch( $column_id ) { 
        case 'last_modified':
            echo get_post_field('post_modified', $post_id);
        break;
        
        case 'modified_author':
            echo get_post_meta($post_id, "meta_value", false);
        break;
   }
}

“上次修改”效果很好,但是我无法显示最后修改者是谁。

我在“wp_postmeta”表中找到>“meta_key”列>_edit_last>

“post_id”显示页面id

"meta_value" 显示最后一个修饰符用户的 id

用于使“上次修改”列可排序。

add_filter( 'manage_edit-page_sortable_columns', 'sortable_page_columns' );
function sortable_page_columns( $columns ) {
    $columns['last_modified'] = 'Last modified';
    return $columns;
}

要使用这些代码,请将它们全部添加到 function.php

【问题讨论】:

    标签: php mysql wordpress


    【解决方案1】:

    试试这个。

    add_filter('manage_pages_columns','bks_add_custom_page_columns');
    function bks_add_custom_page_columns( $columns ) {
        $columns['last_modified'] = 'Last modified';
        $columns['modified_author'] = 'Modified by';
        return $columns;
    }
    
    add_action( 'manage_pages_custom_column','bks_custom_columns_content', 10, 2 );
    function bks_custom_columns_content ( $column_id, $post_id ) {
        switch( $column_id ) {
            case 'last_modified':
                echo get_post_field('post_modified', $post_id);
                break;
    
            case 'modified_author':
                echo the_modified_author(); // Changed.
                break;
        }
    }
    

    我用过the_modified_author()函数。

    the_modified_author() 如果作者 ID 可用,则显示最后编辑当前帖子的作者姓名。

    【讨论】:

    • 正是“the_modified_author()”是我的答案。谢谢
    • @alifallahi,太好了,考虑将此标记为正确答案。谢谢!!
    猜你喜欢
    • 1970-01-01
    • 2012-09-26
    • 2011-04-08
    • 1970-01-01
    • 2011-12-28
    • 1970-01-01
    • 2021-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多