Hot answers tagged

11

My approach. No extra function, no filter. :) <div <?php post_class( 0 === ++$GLOBALS['wpdb']->current_post % 3 ? 'third' : '' ); ?>> Alternative: <div <?php post_class( 0 === ++$GLOBALS['wpdb']->wpse_post_counter % 3 ? 'third' : '' ); ?>>


7

As an addition to @helgathevikings answer Use the post_class() fn without polluting the global namespace Using static variables inside a class allows the same behavior as having global variables: They stay in place and don't change, unless you don't alter them. Even better (as @Milo suggested in the comments), take the current post from the DB class. ...


6

That fact that this concerns post_class is a pretty thin reason to call the question "WordPress related". Mostly it is just PHP but I'll bite. You can make it easy by setting a variable first. $additional_class = (++$j % 2 == 0) ? 'evenpost' : 'oddpost'; ?> <article id="post-<?php the_ID(); ?>" <?php post_class('clearfix span4 ...


6

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 ...


6

Simple fix: <div <?php post_class("clearfix"); ?>> See post_class() for more details


5

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.


5

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 ...


4

I usually include the following parent variable, filter and method in my plugins to account for this case. I've never been sure if this is the "right" way of going about it, but feels better than applying it with javascript. class Plugin_Name { private $parent = 'services'; // ideally this is a setting in your plugin, not a hard-coded variable in case ...


3

The ID can/should be given without quotes (otherwise if you a page with '38034' as slug/post_name, this will be used instead of the page with the ID 38034). And you want to return $classes no matter if you added your own or not. add_filter('body_class', 'custom_body_class'); function custom_body_class($classes) { if (is_page(38034)) $classes[] = ...


3

Here are three methods Approach #1 It's common to wrap the output with custom HTML. We can do that by using the post_gallery filter and the gallery_shortcode() callback: /** * HTML Wrapper - Support for a custom class attribute in the native gallery shortcode */ add_filter( 'post_gallery', function( $html, $attr, $instance ) { if( isset( ...


3

What it does post_class adds classes to the posts on a post by post basis, usually adding them to some kind of post "wrapper". This allows for the targeting of posts with CSS rules according to various criteria-- category, tag, sticky, format, etc. That is, theme authors, plugin authors, and individual users can write CSS rules based on the classes added by ...


3

Use get_post_class() when you want the values to be returned rather than echod.


3

post_class() echos so that's you're problem. From the codex: If you would prefer to have the post classes returned instead of echoed, you would want to use get_post_class(). So just do this: $the_post_classes = get_post_class( 'clearfix' ); $the_post_class_string = ''; foreach( $the_post_classes as $post_class ) { $the_post_class_string .= ...


2

There's a filter for that. Example: function wpse_filter_post_class( $classes ) { // How you determine what class is up to you; // We will assume you've determined the class name // and added it to $my_post_class $my_post_class = 'some-class'; // Add it to the array of post classes $classes[] = $my_post_class; // Return the ...


2

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: // ...


2

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( ...


2

Multiple post classes should be separated by a single space or should be in an array. post_class("$even_odd $current_post $current_in_total"); Or post_class(array($even_odd, $current_post, $current_in_total));


2

This could be solved via CSS as well. Just set up declarations for the special cases where there is overlap between categories: .category-cars { background-color: #f00; } .category-bikes, .category-cars.category-bikes { background-color: #0f0; } This will give any posts that are in both bikes and cars the same background color as those used in the bikes ...


2

I realised that $wp_query doesn't work for where I had customised the queries with WP_query. This is completely obvious I suppose when you know, but I didn't, so here is the adjusted code in case it benefits some other amateur! Note that $media_query, $events_query etc are the instances of WP_query which I had created myself on the respective archive pages ...


2

sanitize_html_class sanitizes a html classname to ensure it only contains valid characters. foreach((get_the_category($post->ID)) as $category) $classes [] = sanitize_html_class($category->cat_name);


2

There is no need to add anything via WordPress, you can simple add the following rule to your CSS file: a[href$='.pdf'] {} This will target all links pointing to a URI ending in .pdf. This works in all modern browsers and from IE7 upwards.


2

Perhaps something like: function mytheme_get_post_class_parent_cats() { $parent_cat_classes = array(); // Get parent categories $post_cats = wp_get_post_categories(); // Loop through post categories foreach ( $post_cats as $post_cat ) { $cat_parents = get_category_parents( $post_cat , false, ',', true ); // if there are ...


2

post_class() will echo the class in the format class="postclassA postclassB" get_post_class() will return an array of all registred classes To use get_post_class() inside sprintf(), you have to return the array as a string. For this purpose you can use the join() function. Example $classes = join(' ', get_post_class()) $sidebar = sprintf('<div ...


2

That is because the get_the_tags() function returns an array with the tag id as the key value. You could use array_values( get_the_tags( $post->ID ) ). That should do the trick.


2

$i = 0; if ( have_posts ) : while( have_posts ) : the_post(); $class = 'class="BASIC-CLASS'; if ( 0 === ( $i % 3 ) ) $class .= 'YOUR-SPECIAL-CLASS'; $class .= '"'; echo "<div {$class}>"; // do stuff echo '</div>'; $i++; endwhile; endif;


2

if your theme uses post_class() to generate post classes you could try. i'm not 100% sure how it will handle pagination b/c i don't have enough posts on my local install to test it out add_filter('post_class','wpa_44845'); global $current_count; $current_count = 1; function wpa_44845( $classes ){ global $current_count; if ($current_count %3 ...


2

A bit shorter and easier: implode (' ', get_post_class( 'clearfix' ) ) This just makes it shorter and allows you to be rid of the pesky space at the end of the class.


2

The list you are referring to was done start to mid 2013, which can be a bit outdated as the was major improvements/new feutures like The TINYMCE editor undergone major modifications and upgrades as from version 3.9 Version 4 saw the introduction of new shortcodes like the video shortcode and audio playlist, just to name a few In the back end plenty of new ...


2

It just take a little bit more complicated logic: function alternating_post_class($classes) { static $counter = 1; switch ($counter) { case 1: $classes[] = 'full-width'; break; case 2: case 3; $classes[] = 'half-width'; break; } $counter = ($counter == 3) ? 1 : $counter + 1; return $classes; } ...


2

For each extra functionality, you are going to pay for How much would depend on the function or action. It is like @TomJNowell said, even a simple echo statement costs time and memory although it is really minute and insignificant. The real issue here is db calls as they can get very expensive depending on the size and nature of the query and this ...



Only top voted, non community-wiki answers of a minimum length are eligible