脚本宝典收集整理的这篇文章主要介绍了php – 创建多个WordPress类别div,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
<div class="indx-img" style="background-image:url('...');"> <?PHP $testStyle = ""; if(has_category( 'update' ) || has_category( 'uncategorized' )) { $testStyle = "style=\"background-image:url('".get_bloginfo('template_directory')."/img/logo.png')\""; } elseif(has_category( 'event' )) { $testStyle = "style=\"background-image:url('".get_bloginfo('template_directory')."/img/logo.png')\""; } ?> <div class="test" <?PHP echo $testStyle; ?>></div> </div>
CSS
.indx-img { posITion: relative; padding: 20% 0; } .test { position: absolute; left: 50%; margin-left: -5em; transform: translateY(-50%); -webkit-transform: translateY(-50%); -ms-transform: translateY(-50%); height: 10em; width: 10em; }
要获得任何帖子的所有类别,您需要wp_get_post_categories函数.
$post_categories = wp_get_post_categories( $post_id ); $cats = array(); foreach($post_categories as $c){ $cat = get_category( $c ); $cats[] = array( 'name' => $cat->name,'slug' => $cat->slug ); }
通过这种方式,您可以获得类别列表,并可以相应地应用样式代码.有关wp_get_post_categories用法的详细信息,请参阅https://codex.wordpress.org/Function_Reference/wp_get_post_categories.
以上是脚本宝典为你收集整理的php – 创建多个WordPress类别div全部内容,希望文章能够帮你解决php – 创建多个WordPress类别div所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。