解析WordPress中的post_class与get_post_class函数
《解析WordPress中的post_class与get_post_class函数》要点: PHP应用post_class() post_class 函数描述 函数使用 <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> 以使用为主的函数讲完了, /** * 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 详解 如果你是一个要求不高的人的话,那么 post_class 这个函数其实已经足够你折腾了.如果你是一个有着精神洁癖的人,不想自己的 WordPress 网站有太多无用代码的话,那你可以继续往下看. get_post_class函数详解 比较费解的手册内容如下: tag. <?php get_post_class($class,$post_id); ?> 如果在循环中,并且不需要插入自定义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" } 总结
欢迎参与《解析WordPress中的post_class与get_post_class函数》讨论,分享您的想法,编程之家 52php.cn为您提供专业教程。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |