我使用以下PHP在我的Wordpress主题中显示了许多小部件:
<?php
// A sidebar for widgets, just because.
if ( is_active_sidebar( 'primary-widget-area' ) ) : ?>
<ul>
<?php dynamic_sidebar( 'primary-widget-area' ); ?>
</ul>
<?php endif; ?>
基本上<ul>
中的每个小部件都作为列表项出现。我希望能够将类添加到列表中的最后一个列表项。
我可以用wordpress / php做到这一点吗?
答案 0 :(得分:2)
使用此功能结束。
http://wordpress.org/support/topic/how-to-first-and-last-css-classes-for-sidebar-widgets?replies=1
答案 1 :(得分:1)
我曾编码wordpress为第一个和最后一个小部件添加了第一个和最后一个类名,您可以在the code中找到the ticket。
然而,根据CSS,您应该能够使用:last-child
CSS selector(所有浏览器但不是IE 5.5 - IE 8)来解决最后一项。所以这可能已经解决了你的问题。
ul > li:last-child {border:1em solid red;}
答案 2 :(得分:0)
如果要为第一个或最后一个窗口小部件内侧边栏添加自定义类。
您可以使用动作挂钩&#34; init&#34;
add_action('init', 'ak_add_order_classes_for_widgets' ); function ak_add_order_classes_for_widgets() { global $wp_registered_sidebars, $wp_registered_widgets; #Grab the widgets $sidebars = wp_get_sidebars_widgets(); if ( empty( $sidebars ) ) { return; } #Loop through each widget and change the class names foreach ( $sidebars as $sidebar_id => $widgets ) { if ( empty( $widgets ) ) { continue; } $number_of_widgets = count( $widgets ); foreach ( $widgets as $i => $widget_id ) { $wp_registered_widgets[$widget_id]['classname'] .= ' ct-widget-order-' . $i; # Add first widget class if ( 0 == $i ) { $wp_registered_widgets[$widget_id]['classname'] .= ' ct-widget-first'; } # Add last widget class if ( $number_of_widgets == ( $i + 1 ) ) { $wp_registered_widgets[$widget_id]['classname'] .= ' ct-widget-last'; } } } }
我的博客:http://colourstheme.com/2015/03/add-class-to-first-and-last-widget/