如何将UPDATE_BY栏添加到帖子管理面板?

时间:2019-02-02 作者:Mostafa Norzade

我想在管理面板的发布页面上的一个单独的列中提供每个帖子最后一位编辑的详细信息。

我该怎么做?

1 个回复
最合适的回答,由SO网友:phatskat 整理而成

您将要使用manage_{$post_type}_posts_columns filter 以及manage_{$post_type}_posts_custom_column action:



  function wp2342412_add_updated_by_column( $columns ) {
    // Ensure your column isn\'t already set.
    if ( isset( $columns[\'updated_by\'] ) ) {
        return $columns;
    }

    // Add your column to the end of the columns definition:
    $columns[\'updated_by\'] = __( \'Updated By\', \'your-text-domain\' );
    return $columns;

    /**
      * If you want to insert your column elsewhere, it\'s a bit more involved.
      *
      * This example will insert the column after the Author column.
      * N.B.: You\'ll need to remove lines 20 and 21 to get here!
      */

    // Find the index of the author column, and add 1 to put our column *after* it.
    $author_idx    = array_search( \'author\', array_keys( $columns ) ) + 1;
    $beginning     = array_slice( $columns, 0, $author_idx, true );
    $custom_column = [ \'updated_by\' => __( \'Updated By\', \'your-text-domain\' ) ];
    $ending        = array_slice( $columns, $author_idx, NULL, true );

    $columns = $beginning + $custom_column + $ending;

    return $columns;
 }

add_filter( \'manage_post_posts_columns\', \'wp2342412_add_updated_by_column\', 10 );

function wp432523_populate_updated_by_column( $column_name, $post_id ) {
    // Ensure we only operate on our custom column.
    if ( \'updated_by\' !== $column_name ) {
        return;
    }

    echo esc_html( get_the_modified_author( $post_id ) );
    return;
    // Or, comment out the above two lines (39 and 40) and get *fancy*
    $post = get_post( $post_id );
    $url  = add_query_arg( [
        \'post_type\' => $post->post_type,
        \'author\'    => $post->_edit_last ?: $post->post_author,
    ] );

    echo sprintf( \'<a href="%1$s">%2$s</a>\', $url, get_the_modified_author( $post_id ) );
}

add_action( \'manage_post_posts_custom_column\', \'wp432523_populate_updated_by_column\', 10, 2 );

相关推荐

GET_POSTS在页面模板中工作,但不在短码中工作

我正在尝试编写一个短代码,其中包括“get\\u posts”,以便获取博客帖子数据,然后在页面上显示最近的3篇文章。此代码在模板中工作。然而,当我将其放入输出缓冲区(ob\\u start)内的短代码中时,它无法检索帖子。相反,它会获取当前页面本身并循环浏览该页面(在本例中为主页)。你知道我怎样才能让它按照最初的意图在帖子中循环吗?以下是在模板中工作的代码:<?php $lastposts = get_posts( array(\'posts_per_page\' => 3) );?>