解析WordPress中的post_class与get_post_class函数_PHP
post_class()
post_class 是 WordPress 内置的一个用于显示文章 class 名称的函数,该函数通常会为每一篇文章生成独一无二的 clss 值,如果你需要制作你自己的主题,而且还需要一点个性的话,那你最好驻足一下,使用该函数并配合灵活的 css 代码,我们可以制作出个性化十足的 WordPress 博客。
post_class 函数描述
该函数通常会为每一篇文章生成独一无二的 clss 值,可以很方便使用于文章所在的节点中。
函数使用
向其他的诸如 header_image、wp_title这样的 WordPress 标签函数一样,不带 get 的函数通常是会直接显示出来而不返回任何值。
<post id="post-<?php the_ID(); ?>" <?php post_class(); ?> > <?php the_content ;?> </post>
是的,也许你已经注意到了,使用 post_class 函数时我们甚至不需要这样去写 clss=“post_class()”;。
实例结果
不卖关子,结果如下
<post id="post-888" class="post-888 post type-post status-publish format-standard hentry category-2 tag-wordpress" > 文章内容 </post>
以使用为主的函数讲完了,
下面照旧给出函数源代码:
想要了解更多关于该函数,以及get_post_class函数请关注后期文章。
/** * Display the classes for the post div. * * @since 2.7.0 * * @param string|array $class One or more classes to add to the class list. * @param int $post_id An optional post ID. */ function post_class( $class = '', $post_id = null ) { // Separates classes with a single space, collates classes for post DIV echo 'class="' . join( ' ', get_post_class( $class, $post_id ) ) . '"'; }
get_post_class 详解
get_post_class 是 post_class 函数的基本实现,在 WordPress 中其他一些带 get 的函数一样,该函数将会有一个返回值,而该返回值将是一个包含当前文章基本信息的数组,get_post_class 函数主要用来给每篇文章生成独一无为的 class 值而被封装出来的。
如果你是一个要求不高的人的话,那么 post_class 这个函数其实已经足够你折腾了。如果你是一个有着精神洁癖的人,不想自己的 WordPress 网站有太多无用代码的话,那你可以继续往下看。
get_post_class函数详解
该函数主要用来生成一个当前文章相关信息的数组,该数组所含信息我们往往用来作为文章层中的 class 值。
就像我上面提到的 post_class 函数,就是利用了本函数生成的 class 值。
并且该函数支持插入你自己的 class 值,一合并到返回数组中。
以上是我本人的理解,当然你也可以看一下官方的手册。
比较费解的手册内容如下:
WordPress Themes have a template tag for the post HMTL tag which will help theme authors to style more effectively with CSS. The Template Tag is called get_post_class. This function returns different post container classes which can be added, typically, in the index.php, single.php, and other template files featuring post content, typically in the HTML
tag.
函数用法
<?php get_post_class($class, $post_id); ?>
如果在循环中,并且不需要插入自定义class值的话,该函数可不接受任何参数。
函数参数
$class:自定义 class 值,可以使字符串也可以死数组。
$post_id:文章ID
使用实例
$MyClass = get_post_class(); var_dump($MyClass);
输出结果:
array(9) { [0]=> string(8) "post-249" [1]=> string(4) "post" [2]=> string(9) "type-post" [3]=> string(14) "status-publish" [4]=> string(15) "format-standard" [5]=> string(6) "hentry" [6]=> string(18) "category-catcatcat" [7]=> string(8) "tag-tag1" [8]=> string(8) "tag-tag2" }
进阶实例
$MyClass = get_post_class('index-post',249); //或 $MyClass = get_post_class(array( 'index-post'),249); var_dump($MyClass);
输出结果:
array(10) { [0]=> string(8) "post-249" [1]=> string(4) "post" [2]=> string(9) "type-post" [3]=> string(14) "status-publish" [4]=> string(15) "format-standard" [5]=> string(6) "hentry" [6]=> string(18) "category-catcatcat" [7]=> string(8) "tag-tag1" [8]=> string(8) "tag-tag2" [9]=> string(10) "index-post" }
总结
根据函数的源代码,我们可以看出,本函数 class 值罗列的顺序为:
- 文章id
- 文章类型(页面、文章)
- 文章类型(页面、文章)与上一条相同,但结果中多了‘type-'字样
- 发布状态
- 文章格式
- 是否需要密码
- 文章所述分类(会逐个罗列所述分类)
- 文章所述标签(会逐个罗列标签)

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

PHP和Flutter是移动端开发的流行技术。Flutter胜在跨平台能力、性能和用户界面,适合需要高性能、跨平台和自定义UI的应用程序。PHP则适用于性能较低、不跨平台的服务器端应用程序。

通过编辑 style.css 文件,您可以轻松修改 WordPress 页面宽度:编辑 style.css 文件,添加 .site-content { max-width: [您的首选宽度]; }。修改 [您的首选宽度] 以设置页面宽度。保存更改并清除缓存(可选)。

在 WordPress 中创建产品页面:1. 创建产品(名称、描述、图片);2. 自定义页面模板(添加标题、描述、图片、按钮);3. 输入产品信息(库存、尺寸、重量);4. 创建变体(不同颜色、尺寸);5. 设置可见性(公开或隐藏);6. 启用/禁用评论;7. 预览并发布页面。

WordPress 文章存储在 /wp-content/uploads 文件夹中。该文件夹使用子文件夹对不同类型的上传进行分类,包括按年、月和文章 ID 组织的文章。文章文件以纯文本格式 (.txt) 存储,文件名通常包含其 ID 和标题。

WordPress 模板文件位于 /wp-content/themes/[主题名称]/ 目录。它们用于决定网站的外观和功能,包括页眉(header.php)、页脚(footer.php)、主模板(index.php)、单个文章(single.php)、页面(page.php)、存档(archive.php)、类别(category.php)、标签(tag.php)、搜索(search.php)和 404 错误页面(404.php)。通过编辑和修改这些文件,可以自定义 WordPress 网站的外

在 WordPress 中搜索作者:1. 登录管理面板后,导航到“文章”或“页面”,使用搜索栏输入作者姓名,在“过滤器”中选择“作者”。2. 其他技巧:使用通配符扩大搜索范围,使用运算符组合条件,或输入作者 ID 以搜索文章。

最稳定的 WordPress 版本是最新版本,因为它包含最新的安全补丁、增强性能并引入新特性和改进。为了更新到最新版本,请登录 WordPress 仪表盘,转到“更新”页面并单击“立即更新”。

WordPress 采用 PHP 语言开发,作为其核心编程语言,用于处理数据库交互、表单处理、动态内容生成和用户请求。PHP 被选择的原因包括跨平台兼容性、易于学习、活跃社区以及丰富的库和框架。除了 PHP,WordPress 还使用 HTML、CSS、JavaScript、SQL 等语言来增强其功能。
