• global $wpdb;
    $posts = $wpdb->get_results($request);
    
    foreach ($posts as $post) {
            setup_postdata($post);
            $post_title = apply_filters('the_title', $post->post_title);
            $comment_count = $post->comment_count;
            $gcont = get_the_content();
            $mdv_most_commented .= '<dt' . post_class('', $post->ID) .'>' . $post_title . ' */ ' . $comment_count . '</dt>' . '<dd class="postcontent">' . $gcont . ' */' . '</dd>';

    now, post_class displays classes for all the posts in one go, why?
    it does not even stay in <dt> tags but jumps out to the front before all the posts’ content, like this:

    class=”post-21 post hentry category-fedora”class=”post-30 post hentry category-films-ive-watched”class=”post-2 page hentry”class=”post-3 post hentry category-films-ive-watched”class=”post-10 post hentry category-music-is-the-panacea”
    intentions */ 1 —- here is first post
    ethernal sunshine of the spotless mind */ 1
    About */ 0
    children */ 0
    myslovitz */ 0

    why is it not working?
    cheers

Viewing 3 replies - 1 through 3 (of 3 total)
  • now, post_class displays classes for all the posts in one go, why?

    Because the function post_class() echoes the classes immediately. You need something that returns the post classes which you can then echo out where appropriate. Try using echo get_post_class($post->ID);.

    Thread Starter lejeczek

    (@lejeczek)

    well, it does not do that in the loop, right? it does display/echos classes of a specific/one post.
    and codex here says, it can be used outside the loop:
    https://codex.www.ads-software.com/Migrating_Plugins_and_Themes_to_2.7
    like this:
    <?php post_class(”,$post_id); ?>

    and yet, it would not work in my bare minimal 2.8.2, why??
    and if you take a closer look at the output I pasted, you can see the post_class in my example does actually outputs/echos classes for all posts the foreach loop finds, but! it does echos it in a way as if it was not in the foreach loop, output does not even end up withing <dt> tags, where I’d expect it to be.
    and the get_post_class returns an array which does not make things any easier.
    cheers

    Thread Starter lejeczek

    (@lejeczek)

    @esmi,
    yes, get_post_class would work though.
    cheers

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘post_class outside the loop??’ is closed to new replies.