Php 将类添加到WordPress仪表板上的自定义帖子类型列表页面

Php 将类添加到WordPress仪表板上的自定义帖子类型列表页面,php,wordpress,Php,Wordpress,我试图在WordPress自定义帖子类型列表页面的每一行中添加一个类,例如,根据后端每个帖子的元字段值添加一个类: 例如:我有一个功能“call enable/disable”-如果帖子被禁用,即只有特定用户才能查看,那么整行应该有特定的背景色 因此,我正在寻找一种基于元字段值添加CSS类的解决方案。我将为您提供一些资料,以便将其混搭起来 首先,检查post类 我们可以通过以下方式进行更改: add_action( 'load-edit.php', function(){ globa

我试图在WordPress自定义帖子类型列表页面的每一行中添加一个类,例如,根据后端每个帖子的元字段值添加一个类:

例如:我有一个功能“call enable/disable”-如果帖子被禁用,即只有特定用户才能查看,那么整行应该有特定的背景色


因此,我正在寻找一种基于元字段值添加CSS类的解决方案。

我将为您提供一些资料,以便将其混搭起来

首先,检查post类

我们可以通过以下方式进行更改:

add_action( 'load-edit.php', function(){
    global $typenow; // current post type
    add_filter( 'post_class', function( $classes, $class, $postID ) {
        $classes[] = 'stack';
        return $classes;
    });
});
要打印CSS,请使用:

add_action( 'admin_head-edit.php', function(){
    $color = 'FDFBC8';
    if( isset( $_GET['color'] ) )
        $color = $_GET['color'];
    ?>
    <style>
    tr.category-uncategorized {
        background-color: #<?php echo $color; ?> !important;
    }
    tr.stack {
        background-color: #333;
    }
    </style>
    <?php
});
此示例在URL中查找?color=hexval:


您将在filter post_类中使用$posted获得元数据。

我将为您提供将其混合的材料

首先,检查post类

我们可以通过以下方式进行更改:

add_action( 'load-edit.php', function(){
    global $typenow; // current post type
    add_filter( 'post_class', function( $classes, $class, $postID ) {
        $classes[] = 'stack';
        return $classes;
    });
});
要打印CSS,请使用:

add_action( 'admin_head-edit.php', function(){
    $color = 'FDFBC8';
    if( isset( $_GET['color'] ) )
        $color = $_GET['color'];
    ?>
    <style>
    tr.category-uncategorized {
        background-color: #<?php echo $color; ?> !important;
    }
    tr.stack {
        background-color: #333;
    }
    </style>
    <?php
});
此示例在URL中查找?color=hexval:


您将在filter post_类中使用$postID获取元数据。

为了防止brasofilo的示例给出缺少参数的警告,您可以使用旧方法使用筛选器:

add_filter('post_class', 'name_of_your_function');
然后你必须定义这个函数。在本例中,我为每一行提供了一个带有post ID的类:

function name_of_your_function($classes)
{
    global $post;   
    $classes[] = 'post-id-' . $post->ID;
    return $classes;
}

为了防止brasofilo的示例向您提供缺少参数警告,您可以使用旧方法使用过滤器:

add_filter('post_class', 'name_of_your_function');
然后你必须定义这个函数。在本例中,我为每一行提供了一个带有post ID的类:

function name_of_your_function($classes)
{
    global $post;   
    $classes[] = 'post-id-' . $post->ID;
    return $classes;
}
functions.php

函数my_post_class$classes,$class$post_id{ $slug=get_post_字段'post_name',$post_id; $error\u slug=false; ifstrpos$slug,“%”!==false{ $error\u slug=true; } ifis_numericsubstr$slug,-1{ $error\u slug=true; } 如果美元错了{ $classes[]=“错误的slug”; } 返回$classes; } 添加过滤器'post_class','my_post_class',10,3; 功能管理风格{ wp_enqueue_style'admin-style',获取_模板_目录_uri.'/admin.css'; } 添加动作“管理队列脚本”、“管理样式”; admin.css

tr.error-slug th,tr.error-slug td{ 背景色:FFCC99; } functions.php

函数my_post_class$classes,$class$post_id{ $slug=get_post_字段'post_name',$post_id; $error\u slug=false; ifstrpos$slug,“%”!==false{ $error\u slug=true; } ifis_numericsubstr$slug,-1{ $error\u slug=true; } 如果美元错了{ $classes[]=“错误的slug”; } 返回$classes; } 添加过滤器'post_class','my_post_class',10,3; 功能管理风格{ wp_enqueue_style'admin-style',获取_模板_目录_uri.'/admin.css'; } 添加动作“管理队列脚本”、“管理样式”; admin.css

tr.error-slug th,tr.error-slug td{ 背景色:FFCC99; }
PHP致命错误:未捕获参数计数器错误:函数{closure}的参数太少,传入1个PHP致命错误:未捕获参数计数器错误:函数{closure}的参数太少,传入1个如果文档说这得到3个参数,为什么会产生参数太少的错误?如果文档说这得到3个参数,为什么会产生太少的参数错误?