WordPress Loop – Style rows of posts differently

It’s basically only some math, but you can use $wp_query properties perfectly for that case: Increment global $wp_query; if ( have_posts() ) { while ( have_posts() ) { the_post(); printf( ‘<div %s>%s</div>’, get_post_class( “style-{$wp_query->current_post}” ), // OR: // get_post_class( “style-“.++$wp_query->wpse66475_increment_posts ) ); } } // endif; So the 1st time, your style-n would increment by … Read more

Adding body class when post contains a specific shortcode

I am doing something like this in one of my plugins: function my_body_class( $c ) { global $post; if( isset($post->post_content) && has_shortcode( $post->post_content, ‘your-shortcode’ ) ) { $c[] = ‘your-class’; } return $c; } add_filter( ‘body_class’, ‘my_body_class’ ); I’m not sure it was really necessary, but I probably can’t remove it now either. TheDeadMedic is … Read more

Alternate post_class on each post

You should add the following code in functions.php: add_filter ( ‘post_class’ , ‘my_post_class’ ); global $current_class; $current_class=”odd”; function my_post_class ( $classes ) { global $current_class; $classes[] = $current_class; $current_class = ($current_class == ‘odd’) ? ‘even’ : ‘odd’; return $classes; } This ensures that all the odd posts on the page will have the class ‘odd’ … Read more

Remove classes from post_class()

One way would be to use preg_match and remove classes that matches given patterns, but… Since we know $post_id, we can use your code with a tiny modification: function lsmwp_remove_postclasses($classes, $class, $post_id) { $classes = array_diff( $classes, array( ‘hentry’, ‘post-‘ . $post_id, ‘type-‘ . get_post_type($post_id), ‘status-‘ . get_post_status($post_id), ) ); return $classes; } add_filter(‘post_class’, ‘lsmwp_remove_postclasses’, … Read more

How can I make wp_list_categories output li with category-slug as class, for its children?

I am not sure if querying the categories again is the good idea. The following code extends the Walker_Category and makes use of it to do the replacement. Put the following in your functions.php: class WPSE67791_Walker_Category extends Walker_Category { public function start_el(&$output, $category, $depth, $args) { parent::start_el( $output, $category, $depth, $args ); $find = ‘cat-item-‘ … Read more

How to add padding between posts

WordPress already generates class for each posts, you can use these classes using the post_class function. Note: The function can be used either within the loop or by passing the $post_id So you would have <ul> <?php $the_query = new WP_Query( ‘posts_per_page=5’ ); ?> <?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?> <div … Read more

Odd/even class on article tag

Try this (untested): <?php $zebra = (++$j % 2 == 0) ? ‘evenpost’ : ‘oddpost’; ?> <article id=”post-<?php the_ID(); ?>” <?php post_class(‘clearfix span4 ‘ . $zebra); ?> role=”article”> You just need to place theodd/even class into a variable before passing it to post_class.

Add filter to wp_list_categories and query what type of taxonomy-terms it use?

WordPress do: apply_filters( ‘wp_list_categories’, $output, $args ); You can do: function add_slug_css_list_categories($list,$args) { } Then you can use $args to determine what kind of list you have. Not sure if you must do that but when you add the filter there is also a variable for accepted arguments: add_filter(‘wp_list_categories’, ‘add_slug_css_list_categories’,10,2);

Where should I use post_class()?

It’s actually only important to use it if your theme’s CSS or JS is reliant on something it outputs. It essentially stuffs relevant class names into the HTML tag you put it on based on the current post/category/blah/blah. So, on a single post template this: <article <?php post_class(); ?>> might output: <article class=”post post-123 category-3 … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)