17713433920 info@mac163.com
WordPress管理面板中的自定义表格单元格颜色
WordPress管理面板中的自定义表格单元格颜色

我一直认为,能够根据WordPress管理中的不同条件来更改背景色帖子行会很有趣。所以,我环顾四周,找到了一些教程来做到这一点。但是不好的是,每个教程都有相同的代码,可能是简单的复制和粘贴。而且,经过一些测试,我发现所有这些帖子都不完整。这就是为什么我决定更深入地解释如何为帖子行添加特定背景色的原因。为此,我们将创建一个新的插件!

步骤1:外挂程式

好的,所以开始时,我们需要创建一个插件,就像我们在之前的文章中看到的那样,这是最简单的部分。在wp-content / plugins中创建一个新文件夹,并将其命名为“ color-my-posts”。在其中,创建一个新文件并调用它。“ color-my-posts.php”(请不要以为我没有想象力!)。打开此文件并添加以下代码:

<?php
/*
Plugin Name: Color My Posts
Plugin URL: http://remicorson.com/color-my-posts
Description: A little plugin to color post rows depending on the posts' status in the administration
Version: 0.1
Author: Remi Corson
Author URI: http://remicorson.com
Contributors: corsonr
tags: color, customization, administration, corsonr, remi corson
*/

这只是告诉WordPress,在plugins文件夹中有一个名为“ Color My Posts”的新插件。现在,即使您的插件为空,也应该在“插件”菜单下的插件列表中列出。在下一步中,我们将创建一个简单的类来实例化插件。

步骤2:主班

当我们在教程中创建了一个有关向WordPress菜单添加自定义属性的类时,我们需要做同样的事情并创建一个类,这是如何做的:

class rc_color_my_posts {

	/*--------------------------------------------*
	 * Constructor
	 *--------------------------------------------*/

	/**
	 * Initializes the plugin
	 */
	function __construct() {

		add_action('admin_footer', array( &$this,'rc_color_my_admin_posts') );

	} // end constructor

	function rc_color_my_admin_posts(){
		/* Be patient ! */
	}

}

// instantiate plugin's class
$GLOBALS['color_my_posts'] = new rc_color_my_posts();

在这里,我们正在创建一个名为“ rc_color_my_posts”的类,该类包含一个构造函数和一个名为rc_color_my_admin_posts()的函数。在构造函数中,我们将rc_color_my_admin_posts()函数挂接到admin_footer。这意味着在管理页脚加载过程中将考虑我们的功能。现在,我们要做的就是将CSS代码添加到我们的函数中。此代码将直接打印到WordPress管理页面源代码中。

第3步:帖子的样式

正如我之前所说,我所看到的所有有关在WordPress管理中设置帖子行样式的教程都只涉及帖子状态。没错,您可以为帖子状态逐行设置样式,但不仅限于此。对于那些谁感兴趣的,有一个名为函数get_post_class()在  /wp-includes/post-template.php核心文件。以防万一–不要修改此文件的代码(至少现在也不要)!

好吧,这个get_post_class()函数很棒,因为它是负责确定帖子类的函数。此功能用于管理和前端。这是为get_post_class生成的每个类添加自定义CSS代码的方法:

function rc_color_my_admin_posts(){
 ?>
 <style>
 /* Color by post Status */
 .status-draft { background: #ffffe0 !important;}
 .status-future { background: #E9F2D3 !important;}
 .status-publish {}
 .status-pending { background: #D3E4ED !important;}
 .status-private { background: #FFECE6 !important;}
 .post-password-required { background: #ff9894 !important;}
/* Color by author data */
 .author-self {}
 .author-other {}
/* Color by post format */
 .format-aside {}
 .format-gallery {}
 .format-link {}
 .format-image {}
 .format-quote {}
 .format-status {}
 .format-video {}
 .format-audio {}
 .format-chat {}
 .format-standard {}
/* Color by post category (change blog by the category slug) */
 .category-blog {}
/* Color by custom post type (change xxxxx by the custom post type slug) */
 .xxxxx {}
 .type-xxxxx {}
/* Color by post ID (change xxxxx by the post ID) */
 .post-xxxxx {}
/* Color by post tag (change xxxxx by the tag slug) */
 .tag-xxxxx {}
/* Color hAtom compliance */
 .hentry {}
</style>
 <?php
 }

由于此代码并不复杂,因此我在代码本身中添加了注释。因此,基本上,可以通过以下方式设置行的样式:

  • 发布状态,使用.status-xxx,其中xxx是发布状态
  • 作者,使用.author-self定位您撰写的帖子,并使用.author-other指向其他作者撰写的帖子
  • 发布格式,使用.format-xxx,其中xxx是发布格式
  • 发布类别,使用.category-xxx,其中xxx是类别标签,您可以根据需要定位任意多个类别
  • 帖子类型,使用.xxx或.type-xxx,其中xxx是帖子类型,您可以根据需要定位任意多个CPT
  • 帖子ID,使用.post-xxx,其中xxx是帖子ID,您可以根据需要定位任意数量的帖子
  • 发布标记,使用.tag-xxx,其中xxx是标记段,您可以根据需要定位任意多个标记
  • .hentry用于瞄准hAtom合规性

我没有为所有类设置颜色,但是您可以自己做,并自定义插件以使其看起来像您真正想要的颜色。

粘性帖子呢?

当我使用这个小插件时,我注意到在粘性帖子中没有添加任何类。好吧,至少在行政管理方面。因此,我再次进入了post-template.php文件,发现在粘帖中添加了一个.sticky类,但是仅当is_home()&&!is_paged()…为什么?真的很奇怪 这就是为什么我在WordPress核心轨道上创建票证来提及它的原因。对于那些真的希望能够在管理员中设置粘性帖子样式的人,只需将以下代码添加到get_post_class()函数中,即使我建议您永远不要修改WordPress核心文件:

// sticky for Sticky Posts in administration
if ( is_sticky($post->ID) && is_admin() )
$classes[] = 'post-sticky';

下载插件

对于那些想直接使用即装即用的插件而不必自己创建的人,我将插件添加到存储库中,只需单击此处即可下载。


微信二维码

微信扫描二维码联系我们!
我们在微信上24小时期待你的声音
提供外贸路由器设备产品,轻松翻墙,解答:WP主题推荐,WP网站建设,Google SEO,百度SEO,专业服务器环境搭建等!


需要提供WordPress主题/插件的汉化服务可以随时联系我们!另外成品WordPress网站以及半成品WordPress网站建设,海外Google SEO优化托管服务,百度SEO优化托管服务,Centos/Debian服务器WP专用环境搭建,WP缓存服务器搭建,我们都是你的首选,拥有多年WP开源程序服务经验,我们一直在坚持客户体验,没有最好,只有更好!
回到顶部