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

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

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


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

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

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

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

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

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

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

$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

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

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


2

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


1

Final code: function nav_parent_class($classes, $item) { $cpt_name = 'services'; $parent_slug = 'sluzby'; if ($cpt_name == get_post_type() && !is_admin()) { global $wpdb; // get page info (we really just want the post_name so it can be compared to the post type slug) $page = get_page_by_title($item->title, ...


1

You can conditionally check for post type like this: Code: if ( 'services' == get_post_type( $post->ID ) ) { //your code } See at the Codex: Conditional Tags, but is_singular() does this too.


1

You have to use this format: FUNCTIONS.PHP function theme_name_scripts() { wp_enqueue_script( 'script-name', get_bloginfo('stylesheet_directory') . '/js/min.js', array(), '1.0.0', true ); wp_localize_script( 'script-name', 'ajax', array( 'url' => admin_url( 'admin-ajax.php' ) ) ); } function ajax_callback() { get_event_list(); } ...


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

The easiest and most straight-forward way to do this would be: $tax_terms = get_the_terms( $post->ID, array('genre') ); $tax_terms = wp_list_pluck($tax_terms,'slug'); post_class(implode(' ',$tax_terms)); You could also apply a filter to post_class that does essentially the same. function tax_classes_wpse_105386($classes) { global $post; ...


1

Wordpress does in fact add a current class by default: check where it says: <li id="menu-item-1688" class="current-menu-item"> Within your source code. Now as for the other item, this is a bit tricky. There is two ways to do it assuming that you need the last item for styling. Option #1 is with pure CSS using the last-of-type selector. Option ...


1

Use the post_class filter to test multiple conditions: function wpa_post_class( $classes ) { global $post; if( ! has_post_thumbnail() ) $classes[] = "no-image"; if( get_the_content() == "" ) $classes[] = "no-content"; return $classes; } add_filter( 'post_class', 'wpa_post_class' ); Then your html will just be: <li id="post-<?php ...


1

You should be using post_class (and body_class for that matter) but you don't need it for this. Alter your Loop on your home page to conditionally format the first post. $first = (!is_paged()) ? true : false; if (have_posts()) { while (have_posts()) { the_post(); if ($first) { // code to format the first post $first = false; } else ...


1

It's actually very simple: Use the $wp_query object property current_post. You have to note that it's starting the index with 0, so I'm counting up by one in the example below. if ( have_posts() ) { while( have_posts() ) { the_post(); ?> <article <?php post_class( "post-nr-{$GLOBALS['wp_query']->current_post+1}" ...



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