0

Certain themes don't use the default post class structure in the loop ie

<div class="post-100 post type-post status-publish format-standard hentry category-uncategorized">

Spacing.ca, for example, doesn't use this structure. The posts in their loop use the following tag structure:

<div class="post-listing">

The result is that you cannot add classes to these sorts of posts in the main loop using:

add_filter('post_class', 'my_post_class')

I'd like to be able to add classes to specific posts in the loop given specific criteria (via a plugin im making) and can do it if themes use the standard structure first mentioned, but cannot do it if the theme uses the less typical structure in the 2nd code snippet.

Any thoughts on how I could add classes to posts when the theme uses the 2nd code snippet?

Thanks

1
  • Edit that theme so it does what you want?
    – GhostToast
    Jun 25, 2013 at 19:31

2 Answers 2

3

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 no guarantee that the theme is using those either. If the author neglected something as critical as post_class, who knows what else is left out or hacked.

I can't think of a general purpose solution for cases where the theme is simply doing it wrong. Even output buffering + regex won't work as a general solution as you have no way to reliable find the content section.

Some people treat those classes like "bloat". It is also possible that the theme or a plugin has hooked something that removes those classes.

6
  • I feel like I'm missing something. A lot of themes I see don't have that long class string, they have shorter 'non-default' class strings. I feel like the long string is being replaced by the shorter one along the way. I say this because if I use the post_class() (function rather than filter) then that returns the long class string that I'm looking for which never makes it to front end. Any idea of how/where that might happen as most "premium" themes tend to use this shorter class string on the front end from what I see.
    – bob_hardy
    Jun 25, 2013 at 20:07
  • Just to be clear, I'm using the_post hook. Then when a post meet the criteria I'm looking for I use post_class("newclass") which ends up echoing all that post's classes with "newclass" in there, but it doesn't display these classes inside the appropriate <div class=""> tag
    – bob_hardy
    Jun 25, 2013 at 20:25
  • @bob_hardy : that sounds like something is removing the default classes. See the edit.
    – s_ha_dum
    Jun 25, 2013 at 22:11
  • Is it be possible to add classes after these "bloat" classes have been removed and have these additional classes actually appear in the live HTML? Your help is greatly appreciate!
    – bob_hardy
    Jun 26, 2013 at 14:58
  • 1
    Upon looking into a bunch of themes, it looks like this happens (for the most part) because the theme writer does not use: <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>> as in their loop. They use something like: <div class="post"> Therefore the post_class filter can never be used because the post_class function is never called. And the filter runs when the function is called. No function means no filter..
    – bob_hardy
    Jul 11, 2013 at 20:23
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).

5
  • 2
    People in the office think I'm a magician though :)
    – s_ha_dum
    Jun 25, 2013 at 19:35
  • 1
    Sometimes I think so too (when I look how quick your reputation grows ;) ) Jun 25, 2013 at 19:54
  • I have faith in this community's magic. It's happened before.
    – bob_hardy
    Jun 25, 2013 at 20:09
  • 1
    @KrzysiekDróżdż :) I was thinking more of the requests I get along the lines of "Hey, I need a widget that will let me upload an .indd file and have it show online. Also, people need to able to drag stuff around. Can you do that this afternoon?"
    – s_ha_dum
    Jun 25, 2013 at 22:07
  • @s_ha_dum Yeah, I know what you mean... Jun 26, 2013 at 6:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.