Tag Info

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.


4

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


3

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

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


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[] = ...


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

We're not magicians ;) If there is no function call or hook, then there is no way to do this. (Well, you always can buffer the output and then preg_replace it, but I wouldn't use plugin which do something like that).


2

If themes don't use post_class, which is what generates what you call the "default post class structure", it is going to be difficult, and may be something you have to deal with on a theme by theme basis. There are very few hooks in the Loop itself that you can use, and while things like the_content and the the_title would provide you some hooks there is ...


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

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

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

First of all, <?php post_class(); ?> function prints a string that contains html 'class' attribute. So, whenever you are using this function inside class attribute, then you are actually printing another class attribute inside class attribute. That's why your class is not working. Secondly, since <?php the_content(); ?> function may contains ...


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

This is expected (or at least: designed) behavior. Here is a portion of the Codex on that: The post_class CSS classes appear based upon the post pageview Conditional Tags as follows. Category Category template files and pageviews displaying posts feature the class selectors: post post-id category-ID category-name Of course, you can hook a ...


1

It is very hard to test/debug this kind of question since it is dependent upon a lot of code but it looks to me like you just need to break apart your comma separated list, get the category data, and print the names. get_categories() and wp_list_pluck() should make that pretty easy. //Display category column function column_post_category($item) { $c = ...


1

If you're on a page by default The Loop will only run once so why not wrap your title and content in a container where you can have post_class() available? Start your loop at the top and end it before you get to your footer (or where ever you need to end it). If you are on a blog format and are running the loop multiple times, you'll need to be in the loop ...



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