0

I have created the following query in order to get several post types in a custom page, which so far works:

<?php
            $args = array(
                'post_type' => array('things', 'people', 'post'),
                'posts_per_page' => -1,
            );

            $query = new WP_Query( $args );
            $postcount = 0;
        ?>

        <?php if ($query->have_posts()) : ?>
        <?php while ($query->have_posts()) : $query->the_post(); ?>

             <?php the_title( '', '', true ); ?>

        <?php $count++; // Increase the count by 1 ?>       
        <?php endwhile; endif; // END the Wordpress Loop ?>
        <?php wp_reset_query(); ?>

I want to have each post type with a class, or several classes. If it is a people custom post type, add the class people around. How can I do it?

3

The post_class function will do this for you automatically within the loop:

<div <?php post_class(); ?>>

which will add some combination of these classes:

  • .post-[id]
  • .[post-type]
  • .type-[post-type]
  • .status-[post-status]
  • .format-[post-format] (default to 'standard')
  • .post-password-required
  • .post-password-protected
  • .has-post-thumbnail
  • .sticky
  • .hentry (hAtom microformat pages)
  • .[taxonomy]-[taxonomy-slug] (includes category)
  • .tag-[tag-name]

You can also get just the post type within the loop via the get_post_type function:

<?php echo get_post_type(); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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