我正在使用Hybrid core framework尝试添加我的类item
在…上hybrid_post_attributes
使用此代码的筛选器生成的类。
add_filter( \'hybrid_post_attributes\', \'my_post_atributes\');
function my_post_atributes($attributes){
return $attributes[\'class\'] = substr_replace($attributes[\'class\'], \' item\', -1);
}
我希望将我的“item”类添加为“hybrid\\u post\\u attributes”类中的最后一个类。这是
hybrid_post_attributes
作用
function hybrid_post_attributes() {
$attributes = array();
$output = \'\';
$attributes[\'id\'] = \'post-\' . get_the_ID();
$attributes[\'class\'] = join( \' \', hybrid_get_post_class() );
$attributes = apply_atomic( \'post_attributes\', $attributes );
foreach( $attributes as $attr => $value )
$output .= " {$attr}=\'{$value}\'";
echo $output;
}
这里是hybrid\\u get\\u post\\u类函数。
function hybrid_get_post_class( $class = \'\', $post_id = null ) {
static $post_alt;
$post = get_post( $post_id );
/* Make sure we have a real post first. */
if ( !empty( $post ) ) {
$post_id = $post->ID;
/* Add hentry for microformats compliance, the post type, and post status. */
$classes = array( \'hentry\', $post->post_type, $post->post_status );
/* Post alt class. */
$classes[] = \'post-\' . ++$post_alt;
$classes[] = ( $post_alt % 2 ) ? \'odd\' : \'even alt\';
/* Author class. */
$classes[] = \'author-\' . sanitize_html_class( get_the_author_meta( \'user_nicename\' ), get_the_author_meta( \'ID\' ) );
/* Sticky class (only on home/blog page). */
if ( is_home() && is_sticky() && !is_paged() )
$classes[] = \'sticky\';
/* Password-protected posts. */
if ( post_password_required() )
$classes[] = \'protected\';
/* Has excerpt. */
if ( post_type_supports( $post->post_type, \'excerpt\' ) && has_excerpt() )
$classes[] = \'has-excerpt\';
/* Has <!--more--> link. */
if ( !is_singular() && false !== strpos( $post->post_content, \'<!--more-->\' ) )
$classes[] = \'has-more-link\';
/* Post format. */
if ( current_theme_supports( \'post-formats\' ) && post_type_supports( $post->post_type, \'post-formats\' ) ) {
$post_format = get_post_format( $post_id );
$classes[] = ( ( empty( $post_format ) || is_wp_error( $post_format ) ) ? \'format-standard\' : "format-{$post_format}" );
}
/* Add category and post tag terms as classes. */
if ( \'post\' == $post->post_type ) {
foreach ( array( \'category\', \'post_tag\' ) as $tax ) {
foreach ( (array)get_the_terms( $post->ID, $tax ) as $term ) {
if ( !empty( $term->slug ) )
$classes[] = $tax . \'-\' . sanitize_html_class( $term->slug, $term->term_id );
}
}
我使用过滤器的方式是否错误<谢谢。
SO网友:s_ha_dum
要使过滤器正常工作,混合核心代码必须运行以下内容$abcd = apply_filters(\'hybrid_post_attributes\',$something);
我在你发布的代码中看不到这一点。事实上,我不明白apply_filters
任何地方都可以。完全基于发布的代码,您无法过滤这些项目。
我想你不明白过滤器是怎么工作的。不能挂钩到函数。您需要连接到专门使用apply_filters
或do_action
(用于行动挂钩)。请参见:
Clarification on filters and hooks
http://codex.wordpress.org/Plugin_API
然而,根据名字,我猜测hybrid_get_post_class
本身就与post_class
. 您应该能够将自己的函数与此挂钩。
add_filter(\'post_class\', \'my_post_attributes\');
function my_post_attributes($classes){
$classes[] = \' item\';
return $classes;
}
这是你的“一行”版本@ChipBennett。
add_filter(\'post_class\', \'my_post_attributes\');
function my_post_attributes($classes){
return array_merge($classes,array(\'item\'));
}