【问题标题】:Custom Columns Don't Display Anything自定义列不显示任何内容
【发布时间】:2017-08-26 23:17:19
【问题描述】:

我在自定义帖子类型中添加了一个新列以显示帖子的 ID。这适用于 WordPress 核心帖子类型,但不适用于我的自定义帖子类型。我尝试使用 manage_{post_type}_custom_column 钩子并将其应用于所有帖子,但都不起作用。

它确实添加了自定义列标题,但在查看自定义帖子类型时我根本无法用任何内容填充它们。

This is what it looks like when viewing the custom post typethis is what it looks like when viewing a regular core post.

//  Add post ID column to use an order ID in all posts view.

add_filter('manage_posts_columns', 'oms_order_id_header');
add_action('manage_posts_custom_column', 'oms_order_id_column', 10, 2);

function oms_order_id_header($columns) {
    //Remove title column
    //unset($columns['title']);
    //Add new columns
    $columns['order_id_header'] = 'Order ID';
    $columns['customer_header'] = 'Customer';
    $columns['author']          = 'Owner';    

    return $columns;
}

function oms_order_id_column( $column, $post_id ) {
        if ($column == 'order_id_header') {
            echo $post_id;
        }
}

【问题讨论】:

    标签: wordpress custom-wordpress-pages wordpress-hook


    【解决方案1】:

    这原来是帖子类型设置为分层的问题。分层帖子类型需要通过与此处使用的不同的操作挂钩来定位。

    分层帖子类型需要使用manage_pages_custom_column,而不是manage_posts_custom_column

    【讨论】:

      【解决方案2】:

      我刚刚尝试了你的代码,它似乎在我的 WordPress 安装中在两种情况下都能完美运行:自定义帖子和帖子。

      也许您的错误是因为您的帖子是草稿而发生的?我不这么认为,但也许。 (我也在我的安装草稿中尝试了你的代码并且它有效)。截图如下:

      尝试打印“hello world”而不是 $post_id 以检查它是否在所有情况下都打印。

      【讨论】:

        猜你喜欢
        • 2014-09-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-27
        • 2016-03-11
        • 1970-01-01
        相关资源
        最近更新 更多