Title: get_post_class
Last modified: August 20, 2016

---

# get_post_class

 *  Resolved [jasonsweb](https://wordpress.org/support/users/jasonsweb/)
 * (@jasonsweb)
 * [13 years, 5 months ago](https://wordpress.org/support/topic/get_post_class/)
 * Hi,
 * I’ve read the WordPress codex and Googled for hours, but I can’t get the “get_post_class”
   to work in my shortcode.
 * I would like to convert this code into a shortcode valid code:
    `<div id="post-
   1027" <?php post_class('custom-class'); ?>>`
 * At the moment I have this:
    `<div id="post-1027" ' . get_post_class('custom-class',
   $post->ID) . '>`
 * But unfortunately this returns an empty class, instead of the complete post classes.
 * What am I doing wrong here?
    Thanks in advance!

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [13 years, 5 months ago](https://wordpress.org/support/topic/get_post_class/#post-3259409)
 * [http://codex.wordpress.org/Function_Reference/get_post_class](http://codex.wordpress.org/Function_Reference/get_post_class)
 * the function returns an array of post_classes; you need to convert them to a 
   string and add the `'class=" "'` part;
 * example (untested):
 *     ```
       <div id="post-1027" class="' . implode(' ',get_post_class('custom-class', $post->ID)) . '">
       ```
   
 * [http://php.net/manual/en/function.implode.php](http://php.net/manual/en/function.implode.php)
 * (if you have problem with getting the right post_classes, you might also need
   to add `global $post;` at the start of the shortcode function)
 *  Thread Starter [jasonsweb](https://wordpress.org/support/users/jasonsweb/)
 * (@jasonsweb)
 * [13 years, 5 months ago](https://wordpress.org/support/topic/get_post_class/#post-3259443)
 * Thanks!
    Tested your code and it works perfect!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘get_post_class’ is closed to new replies.

## Tags

 * [shortcode](https://wordpress.org/support/topic-tag/shortcode/)

 * 2 replies
 * 2 participants
 * Last reply from: [jasonsweb](https://wordpress.org/support/users/jasonsweb/)
 * Last activity: [13 years, 5 months ago](https://wordpress.org/support/topic/get_post_class/#post-3259443)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
